博客已持续运行3674天
每个优秀的人都有一段沉默的时光,那段时光,是付出了很多努力,却得不到结果的日子,我们把它叫做扎根。
进入博客
不要停下脚步哪怕前方没有光
2.18
2017
Day3 笔记练习 选择结构if语句和Switch语句的区别
 
import java.util.Scanner;
class Test3_SwitchIf {
	public static void main(String[] args) {
		/*Scanner sc = new Scanner(System.in);
		System.out.println("请输入您想查询月份的季节!范围[1-12]");
		/*
		一年有四季
		3,4,5 春季
		6,7,8 夏季
		9,10,11 秋季
		12,1,2 冬季
		
		int x = sc.nextInt();
		switch (x) {
		case 1:
			System.out.println("冬季");
		break;
		case 2:
			System.out.println("冬季");
		break;
		case 3:
			System.out.println("春季");
		break;
		case 4:
			System.out.println("春季");
		break;
		case 5:
			System.out.println("春季");
		break;
		case 6:
			System.out.println("夏季");
		break;
		case 7:
			System.out.println("夏季");
		break;
		case 8:
			System.out.println("夏季");
		break;
		case 9:
			System.out.println("秋季");
		break;
		case 10:
			System.out.println("秋季");
		break;
		case 11:
			System.out.println("秋季");
		break;
		default:
			System.out.println("您输入了范围之外的值!");
		
		}



		=====================================
		再效率些的方法:利用case穿透: 345   678   9 10 11   12 1 2
		
		case 3:
		case 4:
		case 5:
			System.out.println("春季");
		break;
		case 6:
		case 7:
		case 8:
			System.out.println("夏季");
		break;
		=====================================
		*/








		//第二种方式If语句





		Scanner sc = new Scanner(System.in);
		System.out.println("请输入您想查询月份的季节!范围[1-12]");
		int x = sc.nextInt();
		if (x <= 2) {
			System.out.println("冬季");
		}else if (x <= 5 && x >= 3) {
			System.out.println("chunji");
		}else if (x <= 8 && x >= 6) {
			System.out.println("夏季");
		}else if (x <= 9 && x >= 11) {
			System.out.println("秋季");
		}else if (x == 12) {
			System.out.println("冬季");
		}else{
			System.out.println("输入的值不在范围 ");
		}






	/*=================================
	if (x > 12 || x < 1) {
			System.out.println("没有对应的季节");
		}else if (x <= 5 && x >= 3) {
			System.out.println("chunji");
		}else if (x <= 8 && x >= 6) {
			System.out.println("夏季");
		}else if (x <= 9 && x >= 11) {
			System.out.println("秋季");
		}else {
			System.out.println("冬季");
		}
	}
		此方法为老师的方法,代码简洁高效.
		应该膜拜学习
	//=================================*/
	}
}
上一篇
回到顶部
Powered by 顾羽宸. Written by . 浙icp备87656888号
在路上...     30.19ms