for(i=1; i s = i*i*i; // 語法錯誤:缺少條件表達式與增量表達式
while(s>0) { ... }
for 語法結構為 for(初始化; 條件; 增量) 語句。代碼中:
i s = i*i*i(非布爾表達式)。while 被視為循環體的單條語句,邏輯混亂。
public static void main(String[] args) {
int count = 0;
for (int i = 1; i < 1000; i++) { // 正確的 for 結構
int sum = 0;
int temp = i;
while (temp > 0) {
int digit = temp % 10;
sum += digit * digit * digit;
temp /= 10;
}
if (sum == i) {
count++;
System.out.println(i);
}
}
System.out.println("總數: " + count);
}
1 153 370 371 407 總數: 5