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。
添加回答
舉報