我正在尝试编写一个简单且无用的程序来生成所有整数 1><1000 的列表,其中数字总和为 11。每次运行时,我都会陷入无限循环。我尝试了不同的东西 - for(){}
, while(){}
,在循环计数器达到 500 后添加 aif(count>500){break;}
来停止它......仍然没有......我在哪里出错了?
提前致谢
//loops through all numbers whose sum of digits is 11
for(int number = 29; number < 1000; number++) {
//checks the values of the 100,10,and 1 position
int hPlace = number / 100; number = number - (hPlace * 100);
int tPlace = number / 10; number = number - (tPlace * 10);
int oPlace = number;
//sum of digits
int i = hPlace + tPlace + oPlace;
//prints if sum of digits is 11
int count = 0;
if (i == 11) {
count++;
System.out.print(i + " ");
}
//new line after every 10 numbers -- just for formatting
if (count % 10 == 0) {
System.out.println("");
}
}