第二章第十八题(打印表格)(Print a table)
程序员文章站
2024-02-29 18:48:52
...
2.18(打印表格)编写程序,显示下面的表格。将浮点数值类型转换为整数。
a | b | pow(a,b) |
---|---|---|
1 | 2 | 1 |
2 | 3 | 8 |
3 | 4 | 81 |
4 | 5 | 1024 |
5 | 6 | 15625 |
2.18(Print a table) Write a program that displays the following table.convert double to int.
a | b | pow(a,b) |
---|---|---|
1 | 2 | 1 |
2 | 3 | 8 |
3 | 4 | 81 |
4 | 5 | 1024 |
5 | 6 | 15625 |
下面是参考答案代码:
public class PrintPowTableQuestion18 {
public static void main(String[] args) {
int a,b,PowerResult;
System.out.println("a b pow(a,b)");
a = 1;b = 2;PowerResult = (int)Math.pow(a,b);
System.out.println(a + " " + b + " " + PowerResult);
a = 2;b = 3;PowerResult = (int)Math.pow(a,b);
System.out.println(a + " " + b + " " + PowerResult);
a = 3;b = 4;PowerResult = (int)Math.pow(a,b);
System.out.println(a + " " + b + " " + PowerResult);
a = 4;b = 5;PowerResult = (int)Math.pow(a,b);
System.out.println(a + " " + b + " " + PowerResult);
a = 5;b = 6;PowerResult = (int)Math.pow(a,b);
System.out.println(a + " " + b + " " + PowerResult);
}
}
运行效果:
注:编写程序要养成良好习惯
如:1.文件名要用英文,具体一点
2.注释要英文
3.变量命名要具体,不要抽象(如:a,b,c等等),形式要驼峰化
4.整体书写风格要统一(不要这里是驼峰,那里是下划线,这里的逻辑段落空三行,那里相同的逻辑段落空5行等等)