日期:2014-05-20 浏览次数:20845 次
public class Lijie01 { public static void main(String[] args){ int i = 1; String cut = "Flip"; String cuc = "Flop"; String cuu = "FlipFlop"; for(;i<=100;i++){ if(i%3==0 && i%5==0){ System.out.println(cuu); } if(i%3 == 0&&!(i%5==0)){//这里 System.out.println(cut); } if(i%5 ==0&&!(i%3==0)){//这里 System.out.println(cuc); } System.out.println(i); } } }
------解决方案--------------------
2楼说的对,if 前面要加else,否则三个就是对立的if了,这样就有可能同时被执行
public class Lijie01 {
public static void main(String[] args){
int i = 1;
String cut = "Flip";
String cuc = "Flop";
String cuu = "FlipFlop";
for(;i<=100;i++){
if(i%3==0 && i%5==0){
System.out.println(cuu);
}
else if(i%3 == 0){
System.out.println(cut);
}
else if(i%5 ==0){
System.out.println(cuc);
}
System.out.println(i);
}
}
}