2 回答

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超6個贊
您從十進(jìn)制到二進(jìn)制的轉(zhuǎn)換是錯誤的,正確的是:
while(num > 0)
{
temp = (int) (num % 2);
binNum = temp + "" + binNum;
num = (int) (num / 2);
}
那么你的 binNum 只需要 9 個字符:
binNum = binNum.length() < 9 ? String.format("%0"+ (9 - binNum.length())+"d%s", 0 ,binNum) : binNum;
// You don't need this inside your loop
binNum = binNum.replaceAll("0", "H");
binNum = binNum.replaceAll("1", "T");
int k=0;
for(int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
coinArr[i][j] = String.valueOf(binNum.charAt(k));
k++;
System.out.print(coinArr[i][j]);
}
System.out.println();
}

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超6個贊
有2個問題:-
1) 改變
binNum = binNum + "" + temp;
到
binNum = temp + binNum;
2) 改變
binNum = String.format((binNum.length() < 9 ? ("%0"+ (9 - binNum.length())+"d%s") : "%0$d%s"), 0 ,binNum);
到
binNum = String.format("%09d", Integer.parseInt(binNum));
添加回答
舉報(bào)