展开全部
我觉得,一旦 if 在循环之外,而 Java 又没有 goto 语句,那么这个 if 就只能使用一次。这个题要 if 里面嵌636f70793231313335323631343130323136353331333337393635 while 那就只能先用 if 找出第一个能被 6 整除的数,然后再用 while 一直加 6 了。然而找出第一个能被 6 整除的数又得用循环。
所以这种想法其没有什么太大意义,顶多能减少一点循环次数。如果非要这样写,要么就一开始就把 x 定成 6,可以只用一个循环:public class CyclePractice1 {
public static void main(String[] args) {
int x = 6;
int count = 0;
if (x % 6 == 0) {
while (x
x += 6;
count++;
}
}
System.out.println("count = " + count);
}
}
要么就先来一个大循环,找出第一个能被 6 整除的 x,然后再 if 套另一个循环。public class CyclePractice1 {
public static void main(String[] args) {
int x = 1;
int count = 0;
while (true) {
if (x % 6 == 0) {
while (x
x += 6;
count++;
}
break;
}
x++;
}
System.out.println("count = " + count);
}
}