java if 嵌套 while_请问这个能改成if里嵌套while吗?(java新手,还望指教)

展开全部

我觉得,一旦 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);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值