while(true) {
a=0;
b=0;
if(condition){
a++;
}else{
b++;
}
}
我有这段代码而且我不知道如何完成它。我希望它在a和b达到一定数量后离开循环。请帮忙。
答案 0 :(得分:4)
a
和b
的声明必须在while
循环之外。否则,它们将始终为0.您可以将while
循环的条件更改为a < amount || b < amount
,这意味着a和b尚未达到该数量。
int a = 0;
int b = 0;
while (a < amount || b < amount) {
if (condition) {
a++;
} else {
b++;
}
}
答案 1 :(得分:2)
使用标志布尔值而不是while(true):
boolean flag = true;
while (flag) {
// your loop
if (a == the amount you want && b == the amount you want)
flag = false;
condition ? a++ : b++;
}
答案 2 :(得分:2)
Assert.assertNotEquals( "", string );
答案 3 :(得分:2)
while (a != some_value || b != some_value2){
a++; b++;
}
答案 4 :(得分:2)
while(true) {
a=0;
b=0;
if(a >= your condition && b >= your condition){
break;
}else{
a++;
b++;
}
}
答案 5 :(得分:1)
尝试
int a = 0;
int b = 0;
while (b < amount) {
if (a < amount) { // increment a if it is less than amount
a++;
} else if(b < amount) { // Now increment b, increment b if it is less than amount
b++;
}
}