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

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

兩個數組的冪(n)

兩個數組的冪(n)

jeck貓 2022-10-20 17:19:21
我的代碼不起作用public static int[] powersOfTwoArray(int n) {    int[] result = new int[n];    int i = 0;    int power = 0;    while (i <= n) {        result[i] = power;        power *= 2;    }    return result;}該方法應返回一個包含 2 從 2 raise 到 0 的冪的數組。
查看完整描述

2 回答

?
互換的青春

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

您的代碼存在三個問題,我在代碼中提到了它們:


public static int[] powersOfTwoArray(int n) 

{

    int[] result = new int[n+1];  // use "n+1" otherwise it will throw exception

    int i = 0;

    int power = 1;   // initiate power = 1, not power = 0;

    while (i <= n) 

    {

        result[i] = power;

        power *= 2;

        i++;        // increments "i" otherwise its an infinite loop

    }

    return result;

}


查看完整回答
反對 回復 2022-10-20
?
FFIVE

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

您正在與零相乘,每次都等于零。嘗試

int power = 1;

如果我誤解了,請說明您的問題。


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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