Java_循环语句测试

for:

public class ForTest
{
	public static void main(String args[])
	{
		int i = 0; 
		int sum = 0;

		for (i=1; i<=100; ++i)
		{
			sum += i;
		}

		System.out.println("1+2+3+,,,+100 = " + sum);
	}
}


while:

public class WhileTest
{
	public static void main(String args[])
	{
		int i = 1;
		int sum = 0;
		while (i <= 100)
		{
			sum += i;
			++i;
		}

		System.out.println("1+2+3+,,,+100 = " + sum);
	}
}


do...while:

public class DoWhileTest
{
	public static void main(String args[])
	{
		int i = 1;
		int sum = 0;
		do
		{
			sum += i;
			++i;
		}
		while (i <= 100);

		System.out.println("1+2+3+,,,+100 = " + sum);
	}
}


 

你可能感兴趣的:(Java_循环语句测试)