天天看点

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

如本例所示:

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 = 0

In the loop,i = 1 s = 1

In the loop,i = 2 s = 3

In the loop,i = 3 s = 6

In the loop,i = 4 s = 10

In the loop,i = 5 s = 15

In the loop,i = 6 s = 21

In the loop,i = 7 s = 28

In the loop,i = 8 s = 36

In the loop,i = 9 s = 45

In the loop,i = 10 s = 55

At the end of the loop:11

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