问答详情
源自:4-9 Java循环语句之 for

为什么会输出21?循环条件不是只到20为止吗?

package tiaojian;


public class For {

public static void main (String []args){

int i=0;

while (i<=20){

i++;

System.out.println(i);

}

}


                                                             

}


提问者:慕粉1468733157 2018-09-05 23:44

个回答

  • 宿魇
    2018-09-08 13:30:04

    i为20的时候还是会自增一次的。改为i<20,即19的时候输出就是20了

  • Ooooooops
    2018-09-05 23:58:09

    while(i<=20)改为while(i<20)