java初学者,for循环的使用错误。

    技术2022-05-18  14

     

    如本例所示:

    import java.util.*; public class TestForLoop { public static void main(String args[]) { int i = 0,s = 0; for(i = 0;i <= 10;++i) { s += i; System.out.print("In the loop,i = "+i); System.out.println("/ts = "+s); } System.out.println("At the end of the loop:"+i); } }

     

    在for循环中,定义先做第三个参数的运算,即例子中的i++;然后再进行第二个参数的判断,即例子中的i<=10。

    具体表现为,题目的运行结果为:

    In the loop,i = 0 s = 0In the loop,i = 1 s = 1In the loop,i = 2 s = 3In the loop,i = 3 s = 6In the loop,i = 4 s = 10In the loop,i = 5 s = 15In the loop,i = 6 s = 21In the loop,i = 7 s = 28In the loop,i = 8 s = 36In the loop,i = 9 s = 45In the loop,i = 10 s = 55At the end of the loop:11

     

    一定要谨记这些语法的细节。今天犯了很严重的错误,谨记!

     


    最新回复(0)