亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用while循環格式和邏輯錯誤的功率表

使用while循環格式和邏輯錯誤的功率表

慕森卡 2022-10-20 17:14:49
我正在嘗試制作一個從 1 到 5 的表格,其中顯示最多 6 個值。因此,例如,第 2 列將從 1、2、4、8、16、32、64 開始并停在那里。我無法獲得正確的表格格式。由于數字與它們應該在的位置不一致。例如:我現在面臨的問題是1 2 3 4 51 1 1 1 1 1 1 2 4 8 16 等等等等任何好將不勝感激,我的代碼在下面。 int powNumb=5; int powValue=6;for (int i = 1; i <= powValue; i++) {            System.out.printf("%10d",i);        }        System.out.println();        for (int i = 1; i <= powNumb; i++) {            for (int j = 0; j <=powValue; j++) {                System.out.printf("%10.0f",Math.pow(i, j));            }        }
查看完整描述

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


查看完整回答
反對 回復 2022-10-20
?
偶然的你

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 循環條件下進行切換。


查看完整回答
反對 回復 2022-10-20
?
慕哥9229398

TA貢獻1877條經驗 獲得超6個贊

您的意思是每個元素的基數相同,因此不需要內部循環:


for (int i = 1; i <= powNumb; i++) {

    System.out.printf("%10.0f", Math.pow(powValue, i));

}

這種方式的權力基礎永遠是powValue。


查看完整回答
反對 回復 2022-10-20
  • 3 回答
  • 0 關注
  • 131 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號