Java 中的开关:我可以在案例中包含条件吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31497818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:10:19  来源:igfitidea点击:

Switches in Java: Can I include a condition in a case?

java

提问by Code123

Here is my code:

这是我的代码:

switch(age) {
    case 10:
        System.out.println("You are too young to drive.");
        break;
    case 20:
        System.out.println("You can drive!");
        break;
    default:
        System.out.println("Error");
}

What happens if the age is 15? Well, it gives me an error. So I was wondering if it's possible to include a condition within the case. For example,

如果年龄是 15 岁会怎样?好吧,它给了我一个错误。所以我想知道是否可以在案例中包含一个条件。例如,

case (age>=10 && age<=20):
   System.out.println("You're still too young to drive...");
   break;

I could use an if statement, but I'm wondering if this is possible with a switch.

我可以使用 if 语句,但我想知道这是否可以使用 switch。

回答by Tagir Valeev

No, in Java you cannot do it. Use ifstatement instead. Constructs similar to what you want exist in other languages like Scala.

不,在 Java 中你不能这样做。改用if语句。类似于您想要的构造存在于Scala等其他语言中。

回答by MrMadsen

Because of fall-through you can do this:

由于跌倒,您可以这样做:

switch(age){
    case 10:  
    case 11:
    case 12:
    case 13:
    case 14:
    case 15:
    case 16:
    case 17:
    case 18:
    case 19:
        System.out.println("You are too young to drive.");
        break;  
    case 20:
        System.out.println("You can drive!");
        break;
    default:
        System.out.println("Error");
}

It is a little more code but it accomplishes the same thing.

这是一个多一点的代码,但它完成了同样的事情。

回答by Elliott Frisch

caseRequires a Constant Expression

case需要常量表达式

No. It's not possible because a casemust be a constant expression. But you could (as you guessed) use an if. Logically, anyone under 20(assuming that's the legal age) is too young to drive.

不。这是不可能的,因为 acase必须是一个常量表达式。但是您可以(如您所料)使用if. 从逻辑上讲,任何低于20(假设这是法定年龄)的人都太年轻而不能开车。

final int drivingAge = 20;
if (age < drivingAge) {
    System.out.printf("%d is too young to drive...%n", age);
} else {
    System.out.printf("%d is old enough to drive.%n", age);
}

Alternatively

或者

That could be written with ? :conditional operator(aka a ternary expression; notesome people find the ternary difficult to read) like

这可以用? :条件运算符(又名三元表达式注意有些人发现三元很难阅读)写成

final int drivingAge = 20;
System.out.printf(age < drivingAge ? "%d is too young to drive...%n"
        : "%d is old enough to drive.%n", age);

回答by Will

You can't do this in Java. A switch jumps to the casethat matches the value you're switching on. You can't use expressions of ageinside the case. However, you can use something called a "fallthrough" (see hereand search for "fall through").

你不能在 Java 中做到这一点。一个开关跳转到case与您要打开的值匹配的那个。您不能agecase. 但是,您可以使用称为“ fallthrough”的东西(请参阅此处并搜索“fall through”)。

So, for your example, you could do something like:

因此,对于您的示例,您可以执行以下操作:

switch(age)
{
    case 10:
    case 11:
    case 12:
    case 13:
    case 14:
    case 15:
    case 16:
    case 17:
    case 18:
    case 19:
        System.out.println("You are too young to drive.");
        break;

    case 20:
    case 21:
        System.out.println("You can drive!");
        break;

    default:
        System.out.println("Error");
 }

However, your problem is best solved with an if.

但是,您的问题最好使用if.

回答by Rishabh Singh

Sadly, it's not possible in Java. You'll have to resort to using if-else statements.You can try this, which is very repetitive.

遗憾的是,这在 Java 中是不可能的。你将不得不求助于使用 if-else 语句。你可以试试这个,这是非常重复的。

  switch(age){ 
    case 10: case 11: case 12: case 13: case 14: case 15:
    //And So on
    System.out.println("You are too young to drive.");
        break;
    case 20:
        System.out.println("You can drive!");
        break;
    default:
        System.out.println("Error");
    }

Try if statement

试试 if 语句

   public void canDrive(int age){

   if(age <= 19){
       System.out.println("You are too young to drive.")
   }
   else{
       System.out.println("You can drive!");}
   }