java中switch语句_在Java中使用switch语句进行多项选择

java中switch语句

If your Java program needs to make a choice between two or three actions, an if, then, else statement will suffice. However, the if, then, else statement begins to feel cumbersome when there are a number of choices a program might need to make. There are only so many else...if statements you want to add before the code begins to look untidy. When a decision across multiple options is required, use the switch statement.

如果您的Java程序需要在两个或三个动作之间做出选择,则if,they,else语句就足够了。 但是,当程序可能需要做出许多选择时, if,then,else语句开始变得很麻烦。 在代码开始看起来不整洁之前,您想添加的if ...语句太多了。 当需要跨多个选项做出决定时,请使用switch语句。

切换语句 ( The Switch Statement )

A switch statement allows a program the ability to compare the value of an expression to a list of alternative values. For example, imagine you had a drop-down menu that contained the numbers 1 to 4. Depending on which number is chosen, you want your program to do something different:

switch语句使程序能够将表达式的值与备用值列表进行比较。 例如,假设您有一个包含数字1到4的下拉菜单。根据选择的数字,您希望程序执行其他操作:


//let's say the user picks number 4
int menuChoice = 4;
switch (menuChoice)
{
case 1:
JOptionPane.showMessageDialog(null, "You chose number 1.");
break;
case 2:
JOptionPane.showMessageDialog(null, "You chose number 2.");
break;
case 3:
JOptionPane.showMessageDialog(null, "You chose number 3.");
break;
//This option gets chosen because the value 4 matches the value of
//the menuChoise variable
case 4: JOptionPane.showMessageDialog(null, "You chose number 4."); break;
default:
JOptionPane.showMessageDialog(null, "Something went wrong!");
break;
}

If you look at the syntax of the switch statement you should notice a few things:

如果您查看switch语句的语法,您应该注意以下几点:

1. The variable containing the value that needs to be compared to is placed at the top, inside the brackets.

1.包含需要比较的值的变量放在方括号内的顶部。

2. Each alternative option starts with a case label. The value to be compared against the top variable comes next, followed by a colon. For example, case 1: is the case label followed by the value 1 — it could just as easily be case 123: or case -9:. You can have as many alternative options as you need.

2.每个替代选项均以案例标签开头。 接下来要与top 变量进行比较的值,然后是冒号。 例如,案例1:是案例标签,后跟数值1-就像案例123:或案例-9:一样容易。 您可以根据需要选择任意多个选项。

3. If you look at the above syntax, the fourth alternative option is highlighted — the case label, the code it executes (i.e., the JOptionPane) and a break statement. The break statement signals the end of the code that needs to be executed. If you look, you'll see that every alternative option ends with a break statement. It's very important to remember to put in the break statement. Consider the following code:

3.如果您查看上述语法,则第四个替代选项将突出显示-案例标签,其执行的代码(即JOptionPane)和break语句。 break语句表示需要执行的代码结束。 如果您看一下,您会看到每个替代选项都以break语句结尾。 记住输入break语句非常重要。 考虑以下代码:


//let's say the user picks number 1
int menuChoice = 1;
switch (menuChoice)
case 1:
JOptionPane.showMessageDialog(null, "You chose number 1.");
case 2:
JOptionPane.showMessageDialog(null, "You chose number 2.");
break;
case 3:
JOptionPane.showMessageDialog(null, "You chose number 3.");
break;
case 4:
JOptionPane.showMessageDialog(null, "You chose number 4.");
break;
default:
JOptionPane.showMessageDialog(null, "Something went wrong!");
break;
}

What you expect to happen is to see a dialog box saying "You chose number 1," but because there is no break statement matching the first case label, the code in the second case label also gets executed. This means the next dialog box saying "You chose number 2" will also appear.

您期望发生的是看到一个对话框,显示 “您选择了数字1”,但是由于没有与第一个case标签匹配的break语句,因此第二个case标签中的代码也将被执行。 这意味着下一个对话框将显示“您选择了数字2”。

4. There is a default label at the bottom of the switch statement. This is like a safety net in case none of the values of the case labels match the value being compared with it. It's very useful to provide a way of executing code when none of the desired options are chosen.

4. switch语句的底部有一个默认标签。 这就像一个安全网,如果情况标签的值都不与所比较的值匹配。 当未选择任何所需选项时,提供一种执行代码的方法非常有用。

If you always expect one of the other options to be chosen, then you can leave out the default label, but to put one at the end of every switch statement you create is a good habit to get into. It might seem unlikely that it will ever be used but mistakes can creep into the code and it can help to catch an error.

如果您总是希望选择其他选项之一,则可以省略默认标签,但是在创建的每个switch语句的末尾都放一个标签是一个好习惯。 似乎不太可能使用它,但是错误会蔓延到代码中,并且可以帮助捕获错误。

从JDK 7开始 ( Since JDK 7 )

One of the changes to the Java syntax with the release of JDK 7 is the ability to use Strings in switch statements. Being able to compare String values in a switch statement can be very handy:

JDK 7发行版对Java语法的更改之一是能够在switch语句中使用Strings 。 能够在switch语句中比较String值非常方便:


String name = "Bob";
switch (name.toLowerCase())
{
case "joe":
JOptionPane.showMessageDialog(null, "Good morning, Joe!");
break;
case "michael":
JOptionPane.showMessageDialog(null, "How's it going, Michael?");
break;
case "bob":
JOptionPane.showMessageDialog(null, "Bob, my old friend!");
break;
case "billy":
JOptionPane.showMessageDialog(null, "Afternoon Billy, how's the kids?");
break;
default:
JOptionPane.showMessageDialog(null, "Pleased to meet you, John Doe.");
break;
}

When comparing two String values, it can be a lot easier if you make sure they are all in the same case. Using the .toLowerCase method means all the case label values can be in lowercase.

比较两个String值时,如果确保它们都在相同的情况下,则容易得多。 使用.toLowerCase方法意味着所有大小写标签值都可以小写

关于switch语句要记住的事情 ( Things to Remember About the Switch Statement )

• The type of the variable to be compared against must be a char, byte, short, int, Character, Byte, Short, Integer, String, or enum type.

•要比较的变量的类型必须是char,byte,short,int,Character,Byte,Short,Integer,String或enum类型。

• The value next to the case label cannot be a variable. It has to be a constant expression (e.g., an int literal, a char literal).

•案例标签旁边的值不能是变量。 它必须是一个常量表达式(例如,int文字,char文字)。

• The values of the constant expressions across all the case labels must be different. The following would result in a compile-time error:

•所有案例标签上的常量表达式的值必须不同。 以下内容将导致编译时错误:


switch (menuChoice)
{
case 323:
JOptionPane.showMessageDialog(null, "You chose option 1.");
break;
case 323:
JOptionPane.showMessageDialog(null, "You chose option 2.");
break;

• There can only be one default label in a switch statement.

•switch语句中只能有一个默认标签。

• When using an object for the switch statement (e.g., String, Integer, Character) make sure it is not null. A null object will result in a runtime error when the switch statement is executed.

•将对象用于switch语句时(例如,String,Integer,Character),请确保其不为null。 执行switch语句时,空对象将导致运行时错误。

翻译自: https://www.thoughtco.com/using-the-switch-statement-for-multiple-choices-2033886

java中switch语句

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值