switch 语句

来源:互联网 发布:网络游戏音乐大全 编辑:程序博客网 时间:2024/06/07 08:29

说明:

    A switch statement executes statements based on the value of a variable or an expression.

   The if statement makes selections based on a single true or false condition.Assume there are several cases for the question we're facing.To fully account for all the cases, nested if statements were used. But overuse of nested if statements makes a program difficult to read.To simplify coding for multiple conditions,switch statement is provided.

语法:   

switch (switch-expression) {           case value1: statement(s)1; break;           case value2: statement(s)2; break;            ...           case valueN: statement(s)N; break;           default: statement(s)-for-default;}

注意事项:
      ①The switch-expression must yield a value of char, byte, short, int, or String type and must always be enclosed in parentheses;
      ②The value1, . . ., and valueN must have the same data type as the value of the switch-expression.Note that value1, . . ., and valueN are constant expressions, meaning that they cannot contain variables, such as 1 + x;
      ③When the value in a case statement matches the value of the switch-expression,the statements starting from this case are executed until either a break statement or the end of the switch statement is reached;
      ④The default case, which is optional, can be used to perform actions when none of the specified cases matches the switch-expression;
      ⑤The keyword break is optional. The break statement immediately ends the switch statement.

举例:

① 十二生肖是我国重要的民俗文化,其每12年循环一次,并以12种动物(鼠、牛、虎、兔、龙、蛇、马、羊、猴、鸡、狗、猪)标定其中的每一年。现已知2012年为龙年,试根据某人出生年份给出其属相。

生肖

package Intro_To_Java.Chapter3;import java.util.Scanner;/** * @see 属相判定 * @author JustBeGeek */public class ChineseZodiac {    public static void main(String[] args){        Scanner input = new Scanner(System.in);        System.out.println("请输入年份:");        int year=input.nextInt();        int interval=(year-2012)%12;        if(interval<0){            interval+=12;        }        switch(interval+1){            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;            case 12:System.out.println("兔");break;        }    }      }


② 星座是西方用于占卜的重要方式,试根据某人出生日期给出其星座。

星座

思路:

Step 1 :依据上图绘制判定表如下:

判定表

Step 2 :代码实现

import java.util.Scanner;/** * @see 星座判定 * @author JustBeGeek */public class Horoscope {    public static void main(String[] args){        Scanner input=new Scanner(System.in);        System.out.println("请输入月份:");        int month=input.nextInt();        System.out.println("请输入天数:");        int day=input.nextInt();        String horoscope="";        switch(month){            case 1:if(day>=20)                        horoscope+="水瓶座";                    else                         horoscope+="摩羯座";                     break;            case 2:if(day>=19)                        horoscope+="双鱼座";                    else                         horoscope+="水瓶座";                     break;            case 3:if(day>=21)                        horoscope+="白羊座";                    else                         horoscope+="双鱼座";                     break;            case 4:if(day>=20)                        horoscope+="金牛座";                    else                         horoscope+="白羊座";                     break;           case 5:if(day>=21)                        horoscope+="双子座";                    else                         horoscope+="金牛座";                     break;           case 6:if(day>=22)                        horoscope+="巨蟹座";                    else                         horoscope+="双子座";                     break;              case 7:if(day>=23)                        horoscope+="狮子座";                    else                         horoscope+="巨蟹座";                     break;           case 8:if(day>=23)                        horoscope+="处女座";                    else                         horoscope+="狮子座";                     break;                      case 9:if(day>=23)                        horoscope+="天秤座";                    else                         horoscope+="处女座";                     break;            case 10:if(day>=24)                        horoscope+="天蝎座";                    else                         horoscope+="天秤座";                     break;            case 11:if(day>=23)                        horoscope+="射手座";                    else                         horoscope+="天蝎座";                     break;                case 12:if(day>=22)                        horoscope+="摩羯座";                    else                         horoscope+="射手座";           }        System.out.println("您的星座是"+horoscope);    }}


0 0
原创粉丝点击