要求是1-100之间的数,输出能被5整除的数,3个数换一行。
用FOR写没问题,用WHILE写输出结果第一行出现问题
int o = 1;
while(o<=100){
if(o%5==0){
System.out.print(o+"\t");
}
o++;
if(o%(5*3)==0){
System.out.println();
}
}
输出结果:
5 10 (注:这里不知道为什么10就换行了)
15 20 25
30 35 40
45 50 55...
下面是用FOR写的:
for(int j = 1;j<=100;j++){
if(j%5==0){
System.out.print(j+"\t");
}
if(j%(5*3)==0){
System.out.println();
}
}
输出结果:
5 10 15 (注:这里是正常的)
20 25 30
35 40 45
50 55 60...
o++;
if(o%(5*3)==0){
System.out.println();
}
把o++放到循环最后就好了
你这样写的话不是在10的时候输出换行,而是在14的时候输出的换行。前面的代码执行的o=14,无输出,然后o++;,o变成15,就输出换行了
o++;
if(o%(5*3)==0){
System.out.println();
}
把o++放到循环最后就好了
你这样写的话不是在10的时候输出换行,而是在14的时候输出的换行。前面的代码执行的o=14,无输出,然后o++;,o变成15,就输出换行了
同上
int o = 1;
while(o<=100){
if(o%5==0){
System.out.print(o+"\t");
}
if(o%(5*3)==0){
System.out.println();
}
o++;
}
**解释同上**