為什么sum的值只循環(huán)到28呢
public class Break{
? ?public static void main(String[] args){
? ? ? ?int sum=0;
? ? ? ?for(int x=1;x<10;x++){
? ? ? ? ? sum=sum+x;
? ? ? ? ? if(sum>30){
? ? ? ? ? ? break;
? ? ? ? ? }
? ? ? ? ? ? System.out.println(sum);
? ? ? ?}
? ?}
}
2018-04-24
public class HelloWorld{
? ?public static void main(String[] args){
? ? ? ?int sum=0;
? ? ? ?for(int x=1;x<10;x++){
? ? ? ? ? sum=sum+x;
? ? ? ? ? if(sum>30){
? ? ? ? ? ? ?System.out.println(sum);
? ? ? ? ? ? break;
? ? ? ? ? }
? ? ? ? ? ?
? ? ? ?}
? ?}
}
2017-05-02
當(dāng)循環(huán)到sum=36時(shí),if(sum>30)滿足條件,就會(huì)執(zhí)行語(yǔ)句break,跳出for循環(huán),后面的輸出語(yǔ)句就不會(huì)執(zhí)行
2017-05-01
public class Break{
? ?public static void main(String[] args){
? ? ? ?int sum=0;
? ? ? ?for(int x=1;x<10;x++){
? ? ? ? ? sum=sum+x;
? ? ? ? ? System.out.println(sum);
? ? ? ? ? if(sum>30){
? ? ? ? ? ? break;
? ? ? ? ? }
? ? ? ?}
? ?}
}
把輸出語(yǔ)句和if語(yǔ)句交換一下位置
2017-05-01
因?yàn)槟鉨reak了,break發(fā)生后面就不會(huì)發(fā)生了,所以實(shí)際上循環(huán)到了36只是沒(méi)有輸出而已。你將System.out.println(sum);放到if前就不會(huì)出現(xiàn)這情況了。