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

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

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

<span style="color:#ff0000;">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);
	}
}</span>

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

<span style="color:#ff0000;">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);

	}
}</span>


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

<span style="color:#ff0000;">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);
		
	}
}</span>


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

<span style="color:#ff0000;">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;
			}
		}
	}
}</span>

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

<span style="color:#ff0000;">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);
	}
}
</span>


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


<span style="color:#ff0000;">package Test;

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

	}
}
</span>


7循环执行了几次?

<span style="color:#ff0000;">package Test;

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

8. 循环执行了几次?

<span style="color:#ff0000;">package Test;

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

	}
}</span>

答案:

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.答案:一次也不执行

 


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值