3 回答
TA贡献1898条经验 获得超8个赞
这应该可以帮助你
for (int i = 1; i <= powNumb; i++) {
System.out.printf("%10d", i); //Print the number (1st col)
for (int j = 0; j <= powValue; j++) { //This loop prints the powers of the curent number 'i'
System.out.printf("%10.0f", Math.pow(i, j));
}
System.out.println(); //To end the current row
}
这打印
num num^0 num^1 num^2 ... num^powValue
其中 num 是从 1 到powNumb
输出
1 1 1 1 1 1 1 1
2 1 2 4 8 16 32 64
3 1 3 9 27 81 243 729
4 1 4 16 64 256 1024 4096
5 1 5 25 125 625 3125 15625
TA贡献1841条经验 获得超3个赞
首先,您需要println在内部 for 循环中的某处声明来分隔行。
其次,您需要将呼叫中的iand切换为. 因为按照当前的设置方式,每一行的值都是0 到 6 的幂。例如,第一行是. 然后,第二行将是 但是,您希望第一行是, second row等。所以您的代码应该更改为这样的内容,jMath.powi = row number1^0 1^1 1^2 1^3 1^4 1^5 1^62^0 2^1 2^2 2^3 2^4 2^5 2^61^0 2^0 3^0 4^0 5^01^1 2^1 3^1 4^1 5^1
int powNumb=5;
int powValue=6;
for (int i = 1; i <= powNumb; i++) {
System.out.printf("%10d",i);
}
for (int i = 0; i <= powValue; i++) {
System.out.println();
for (int j = 1; j <=powNumb; j++) {
System.out.printf("%10.0f",Math.pow(j, i));
}
}
输出:
1 2 3 4 5
1 1 1 1 1
1 2 3 4 5
1 4 9 16 25
1 8 27 64 125
1 16 81 256 625
1 32 243 1024 3125
1 64 729 4096 15625
powNumb另外,我必须powValue在 for 循环条件下进行切换。
TA贡献1877条经验 获得超6个赞
您的意思是每个元素的基数相同,因此不需要内部循环:
for (int i = 1; i <= powNumb; i++) {
System.out.printf("%10.0f", Math.pow(powValue, i));
}
这种方式的权力基础永远是powValue。
添加回答
举报