java conditional statement switch-case practice

/ *
Program flow control statements branch statement bis case Switch
Switch configuration
switch (expression) // boolean type expression is not
the case constants 1:
statement 1;
// BREAK
case constants 2:
Statement 2;
// BREAK
case constants 3:
statement. 3;
// BREAK
.
.
.
Case constant N:
statement N;
// BREAK
deafault:
statement;
// BREAK

Description:
1, according to the value of the switch expression, matching word constant in each case, once the match is successful into the
corresponding case structures, which performs call statement, when the call is completed, the case continues downwardly other
structures the implementation of the statement until it encounters the end of the break keyword or switch-case structure so far.
2, break the keyword can be used in switch-case structure, the event represents this keyword will jump switch-case structure
3, the switch expression type specified only has six: byte, short, char, int , enumeration, string.
4, after the case can only declare a constant, you can not be declared range.
5, break the keyword added as needed, in most cases need to
6, default: if-else similar structure else. default configuration is optional.
And Flexibility, not necessarily at the end. At the end of the actual often,

7, if a plurality of switch-case structure in case of performing the same statement can be considered merged. See examples two
principles, the statement did not execute would have been no break to have a break execution place, because the implementation of the same statement, so the same output

8, when a branch structure of formula, if or how to choose a switch. Those using switch can be used if the implementation of the implementation, not vice versa.
When both small and can be used in the case where the switch preferentially selected switch configuration, switch because higher efficiency.

*/

import java.util.Scanner;

class SwitchCaseTest {
	public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
		int num = 2;
		switch(num){
		case 0:
		System.out.println("zero");
		case 1:
		System.out.println("one");
		case 2:
		System.out.println("two");
		case 3:
		System.out.println("three");
		break;//遇到break停止执行
		case 4:
		System.out.println("four");
		default:
		System.out.println("null");//输出two three
		}

		

		num = 1;
		switch(num){		
		case 0:
		System.out.println("zero");
		case 1:
		System.out.println("one");
		case 2:
		System.out.println("two");
		default:
		System.out.println("null");//default位置灵活,但一般不这么写
		case 3:
		System.out.println("three");
		break;
		case 4:
		System.out.println("four");//输出 one two null three
        }



	    num = 5;
		switch(num){
		default:
		System.out.println("null");//default位置灵活,但一般不这么写
		case 0:
		System.out.println("zero");
		case 1:
		System.out.println("one");
		case 2:
		System.out.println("two");
		case 3:
		System.out.println("three");
		break;
		case 4:
		System.out.println("four");//无匹配,执行default,且执行后面语句直到break,
		//输出null zero one two three
	
		}
		
        //例题,对输入的小写字母abcde转换成大写字母输出,其他转换成other。
		System.out.println("请输入一个小写的英文字母");
		String word = scan.next();//scanner不能直接获取char类型,先获取成string类型的再转换
		char c = word.charAt(0);//通过此语句获取string类型的第一个字符为char型
        switch(c){
		case 'a':
		System.out.println("A");
		break;
		case 'b':
		System.out.println("B");
		break;
		case 'c':
		System.out.println("C");
		break;
		case 'd':
		System.out.println("D");
		break;
		case 'e':
		System.out.println("E");
		break;
		default:
		System.out.println("other");
		break;//此处break可以省略

        }

        //例题2,当分数小于60显示不及格,当分数大于等于60显示及格
        System.out.println("请输入得分");
		int score = scan.nextInt();
		// 分数除10,这样就会只剩11种情况,比101种方便

		switch (score / 10){
		case 0 :
		System.out.println("不及格");
		break;
		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;
		default:
		System.out.println("输入非法");
		}

        //因为case执行语句相同可以有省略写法

		switch (score / 10){
		case 0 :
		case 1 :
		case 2 :
		case 3 :
		case 4 :
		case 5 :
		System.out.println("不及格");
		break;
		case 6 :
		case 7 :
		case 8 :
		case 9 :
		case 10 :
		System.out.println("及格");
		break;
		default:
		System.out.println("输入非法");
		}		

        //也可以直接除60这样就只有两种选项0和1

		switch (score / 60){
		case 0 :
		System.out.println("不及格");
		break;
		case 1 :
		System.out.println("及格");
		break;
		default:
		System.out.println("数据非法");
		}

       //例题3,键盘输入2019年的月份month和天数day,输出这是2019年的第几天
	   System.out.println("请输入2019年目前的月份");
	   int month = scan.nextInt();
	   System.out.println("请输入2019年本月的日期");
	   int day = scan.nextInt();
	   int sumDays = 0;
	   switch (month){
	   case 12:
	   sumDays += 30;
	   case 11:
	   sumDays += 31;
	   case 10:
	   sumDays += 30;
	   case 9:
	   sumDays += 31;
	   case 8:
	   sumDays += 31;
	   case 7:
	   sumDays += 30;
	   case 6:
	   sumDays += 31;
	   case 5:
	   sumDays += 30;
	   case 4:
	   sumDays += 31;
	   case 3:
	   sumDays += 28;
	   case 2:
	   sumDays += 31;
	   case 1:
	   sumDays += day; //通过倒加,和 += 的性质来做可以避免冗余代码,更简洁
       break;
	   default:
	   System.out.println("数据非法");
	   }
       
       System.out.println("现在是2019年第" + sumDays + "天。");
       
	   //例题4,分别输入年月日,判断是那一年的第几天,闰年标准:1可以被4整除,但不能被100整除2能被400整除
       System.out.println("请输入年份");
	   int year = scan.nextInt();
	   System.out.println("请输入月份");
	   int month1 = scan.nextInt();
	   System.out.println("请输入几号");
	   int day1 = scan.nextInt();
	   int sumDays1 = 0;
       switch (month1){
	   case 12:
	   sumDays1 += 30;
	   case 11:
	   sumDays1 += 31;
	   case 10:
	   sumDays1 += 30;
	   case 9:
	   sumDays1 += 31;
	   case 8:
	   sumDays1 += 31;
	   case 7:
	   sumDays1 += 30;
	   case 6:
	   sumDays1 += 31;
	   case 5:
	   sumDays1 += 30;
	   case 4:
	   sumDays1 += 31;
	   case 3:
		 if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
            sumDays1 += 29;
		   }else{
			sumDays1 += 28;
		   }  
	   case 2:
	   sumDays1 += 31;
	   case 1:
	   sumDays1 += day1; //通过倒加,和 += 的性质来做可以避免冗余代码,更简洁
       break;
	   default:
	   System.out.println("数据非法");
	   }
	   System.out.println("现在是" + year + "年第" + sumDays1 + "天。");
       
	  //输入一个年份,判断属于十二生肖中的哪一个,输出rat,ox,tiger,dragon,snake,horse,sheep,monkey,rooster,dog,pig
      //2019%12==3,2019年是猪年,2020年是鼠年。
      System.out.println("请输入年份");
	   int year1 = scan.nextInt();
	   String  zodiac = "";	  
       switch (year1 % 12){
	   case 4:
	   zodiac = "鼠rat";
	   break;
	   case 5:
	   zodiac = "牛ox";
	   break;
	   case 6:
	   zodiac = "虎tiger";
	   break;
	   case 7:
	   zodiac = "兔子rabbit";
	   break;
	   case 8:
	   zodiac = "龙dragon";
	   break;
	   case 9:
	   zodiac = "蛇snake";
	   break;
	   case 10:
	   zodiac = "马horse";
	   break;
	   case 11:
	   zodiac = "羊sheep";
	   break;
	   case 0:
	   zodiac = "猴monkey";
	   break;
	   case 1:
	   zodiac = "鸡rooster";
	   break;
	   case 2:
	   zodiac = "狗dog";
	   break;
	   case 3:
	   zodiac = "猪pig";
	   break;
	   default:
	   System.out.println("数据非法");
	   }
	   System.out.println("现在是" + zodiac + "年");




	}
}
Published 45 original articles · won praise 1 · views 2183

Guess you like

Origin blog.csdn.net/wisdomcodeinside/article/details/103751965