java基础学习——循环结构(while,do-while,for)笔试题

                       java基础学习——循环结构(while,do-while,for)笔试题

1.求下面程序的输出结果?

package Test;

public class test {
	public static void main(String args[]) {
		int sum=0;
		for(int i=1;i<=5;i++);
		++sum;
		System.out.println(sum);
	}
}

2.求下面程序的输出结果?

package Test;

public class test {
	public static void main(String args[]) {
		int i=0,x=3;
		while(x<9){
		x+=2;
		    x++;
		    ++i;
		    }

		System.out.println("i="+i);

	}
}


3.求下面程序的输出结果?

package Test;

public class test {
	public static void main(String args[]) {
		int i = 1,j = 10;
		do{
		if(i++>--j)	
		continue;
		}while(i<5);
		System.out.println(i);
		System.out.println(j);
		
	}
}


4.求下面程序的输出结果?

package Test;

public class test {
	public static void main(String args[]) {
		int x = 9;
		for (; x > 0; x--) {
			if (x % 3 == 0) {
				System.out.println(--x);
				continue;
			}
		}
	}
}

5.求下面程序的输出结果?

package Test;

public class test {
	public static void main(String args[]) {
		int a = 1, b = 2, c = 3, t;
		while (a < b && b < c) {
			t = a;
			a = b;
			b = t;
			c--;
		}
		System.out.println(a);
		System.out.println(b);
		System.out.println(c);
	}
}


6.求下面程序的输出结果?


package Test;

public class test {
	public static void main(String args[]) {
		int k = 1;
		while (k++ < 10)
			;
		System.out.println(k);

	}
}

7循环执行了几次?

package Test;

public class test {
	public static void main(String args[]) {
		int  x=-1;
		do{
		   x=x*x;
		}while(!x);
	}
}

8. 循环执行了几次?

package Test;

public class test {
	public static void main(String args[]) {
		int k = 2;
		while (k == 0)
			;
		System.out.println(k);
		k--;

	}
}

答案:

1.答案:1    注意:在for循环后有个;,说明循环里面没关系,不会加1。

2.答案:i=2

3.答案:5 6

4.答案:8  5  2

5.答案:2  1  2

6.答案:11

7.答案:语句有误,whie的里面只能是boolean(true或false)值,不能为其他.

8.答案:一次也不执行

 


                                                                                                                                               希望得到大神的补充,谢谢大家

你可能感兴趣的:(java试题)