水仙花數
/*
統計
需求:
統計"水仙花數"一共有多少個,並在控制檯輸出個數
*/
public class ForTest {
public static void main(String[] args) {
//定義變量count,用於保存“水仙花數”的數量,初始值爲0
int count = 0;
//輸出所有的水仙花數必然要使用到循環,遍歷所有的三位數,三位數從100開始,到999結束
for(int i=100; i<1000; i++) {
//在計算之前獲取三位數中每個位上的值
int ge = i%10;
int shi = i/10%10;
int bai = i/10/10%10;
//在判定水仙花數的過程中,滿足條件不再輸出,更改爲修改count的值,使count+1
if(ge*ge*ge + shi*shi*shi + bai*bai*bai == i) {
count++;
}
}
//打印輸出最終結果
System.out.println("水仙花共有:" + count + "個");
}
}