![](/img/trans.png)
[英]How do I combine to while loops so that I get the same output as I do with two while loops?
[英]How can I combine the below if loops?
我想將以下兩個for循環和對應的if循環與break語句組合在一起。 我怎樣才能做到這一點。
public class Test {
public static void main(String[] args) {
for(int i=0;i<10;i++){
if(i==2){
System.out.println("i2: "+ i);
break;
}
System.out.println("i2: "+ i);
}
for(int i=0;i<10;i++){
if(i==4){
System.out.println("i4: "+ i);
break;
}
System.out.println("i4: "+ i);
}
}
}
嘗試這個
public class Test {
public static void main(String[] args) {
boolean 2completed = false;
boolean 4completed = false;
for(int i=0;i<10;i++){
if(i==2){
System.out.println("i2: "+ i);
2completed = true;
}
else if(i==4){
System.out.println("i4: "+ i);
4completed = true
}
if(!2completed)
System.out.println("i2: "+ i);
if(!4completed)
System.out.println("i4: "+ i);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.