20200219菜鸟学习java之路之循环,选择语句

循环语句:DO While; While;for之间的区别

一: do while和 while的区别

1.do while先执行一遍循环体然后再去判断条件,若条件成立则继续循环,反之则退循环体.(至少循环一次,无论条件成立与否).
2.while是先判断条件是否成立,若成立才会执行循环体;反之则不会执行 (若条件不成立不会执行循环体).`

public class TestDoWhile{
	public static void main(String[] args){
		int x=1;
		do{
		System.out.println("x="+x);
		x++;
		}
		while(x<3);
		testWhile();
	}
	
	
	
	public static void testWhile(){
		int y=1;
		while(y<3){
			System.out.println("y="+y);
			y++;
		}
	}
}

二: while和for之间的区别

1.1变量有自己的作用域,对for循环来讲:若用于控制增量,那么该变量只在for循环语句中有效.for循环执行完毕,该变量在内存中被释放;但while语句中的变量不会被释放.
2.1while语句与for语句可以进行互换,若需定义循环增量用for更为合适

public class TestDoWhile{
	public static void main(String[] args){
		int x=1;
		do{
		System.out.println("x="+x);
		x++;
		}
		while(x<3);
		testWhile();
		testFor();
	}
	
	
	
	public static void testWhile(){
		int y=1;
		while(y<3){
			System.out.println("y="+y);
			y++;
		}
	}
	
	
	public static void testFor(){
		for(int a=1;a<3;a++){
			System.out.println("a="+a);
		}
	}
}

三:选择语句:if; if else; if elseif else; switch之间的区别

1)if语句和switch语句很像,若是判断数值不多时且刚好为 (byte,short,int,char)这四种类型时,建议使用switch语句,其效率稍高.
2)其他情况:如对区间的判断;对结果为double型的判断使用if语句更好,其范围更广.**`
3)各语句的用法:

public class TestSentence{
	public static void main(String [] args){
		int x=1;
		if(x>0){
			System.out.println("yes");
		}
	System.out.println("over");
	testIfElse();
	testIfElseAdElse();
	testSwitch();
	}
	
	
	public static void testIfElse(){
		//测试ifelse语句
			int a=5;
			if(a>5){
		System.out.println("good");
			}
			else 
				System.out.println("bad");
	}
	
	
	public static void testIfElseAdElse(){
		//测试if elseif else语句
		int b=10;
		if(b<0){
		System.out.println("n");
	}
	else if(b>0&&b<10){
		System.out.println("y");
	}
	else 
		System.out.println("ok");
	
	}	
	
	public static void testSwitch(){
		int m=4;
		switch(m){
			case 1:
			System.out.println("a");
			break;
			case 5:
			System.out.println("b");
			break;
			case 4:
			System.out.println("c");
			break;
			case 9:
			System.out.println("d");
			break;
			}
	}
}

四: days2java学习:
要求:
1)分别用选择结构中不同的语句写出春夏秋冬四个季节[3,4,5春;6,7,8夏;9,10,11秋;12,1,2冬]`

public class TestSwitch{
	public static void main(String[] args){
		int a=8;
		switch(a){
		case 3:
		case 4:
		case 5:
		System.out.println("a="+a+"春季");
		break;
		case 6:
		case 7:
		case 8:
		System.out.println("a="+a+"夏季");
			break;
		case 9:
		case 10:
		case 11:
		System.out.println("a="+a+"秋季");
			break;
		case 12:
		case 1:
		case 2:
		System.out.println("a="+a+"冬季");
			break;
			default:
			System.out.ptintln("月份输入错误");
		}
		testIfElseIfElse();
	}
	
	
	
	public static void testIfElseIfElse(){
		int b=8;
		if(b>12 ||b<1)
			System.out.println("月份输入错误");
		else if(b>3&&b<5)
			System.out.println("b="+b+"春天");
		else if(b>6&&b<8)
			System.out.println("b="+b+"夏天");
		else if(b>9&&b<11)
			System.out.println("b="+b+"秋天");
		else 
			System.out.println("b="+b+"冬天");
	}
	
	
}
  2)用循环结构不同的语句写出1-5按顺序输出

public class TestFor{
	public static void main(String[] args){
		for(int a=1;a<6;a++){
		System.out.println("a="+a);
		}
		testWhile();
		testDoWhile();
	}
	
	
	
	
	public static void testWhile(){
		int b=1;
		while(b<6){
			System.out.println("b="+b);
			b++;
		}
	}
	
	
	
	
	public static void testDoWhile(){
			int c=1;
		do{
		System.out.println("c="+c);
		c++;
		}
		while(c<6);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值