Java 你可以在 Android 的 switch-case 中使用条件语句吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25940748/
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 01:33:25  来源:igfitidea点击:

Can you use conditional statements in switch-case in Android?

javaandroidswitch-statement

提问by Robert

I can't seem to find a straight forward yes or no to this in my searching. In Android, is there a way to use a conditional statement in case-switch? For example, with age being an int value:

在我的搜索中,我似乎无法直接确定是或否。在 Android 中,有没有办法在 case-switch 中使用条件语句?例如,age 是一个 int 值:

switch (age){
case (>79):
 // Do this stuff
 break;
case (>50):
 // Do this other stuff
break;
etc, etc

I've tried several ways to code this (completely shooting in the dark) and come up with compiler errors, and I've also tried a nested IF statement, but it doesn't support break so the logic breaks down and it ends up also executing ELSE code lower in the nesting. I feel like switch-case is my best bet but I can't find an example of the correct syntax for what I'm trying to do! Any help would be appreciated. All the examples I find just use switch-case for a few things like if it is a 1 do this, if it's a 2 do that, but without making 100 case statements to check against age, I'm not sure how else to work this.

我尝试了几种方法来对此进行编码(完全在黑暗中拍摄)并提出编译器错误,并且我还尝试了嵌套的 IF 语句,但它不支持 break 所以逻辑崩溃了还在嵌套中执行较低的 ELSE 代码。我觉得 switch-case 是我最好的选择,但我找不到我想要做的正确语法的例子!任何帮助,将不胜感激。我发现的所有示例都只是将 switch-case 用于一些事情,例如如果是 1 则执行此操作,如果是 2 则执行此操作,但是没有进行 100 个 case 语句来检查年龄,我不知道如何工作这个。

采纳答案by Elliott Frisch

No. You cannot do this,

不,你不能这样做,

switch (age){
case (>79):
  // Do this stuff
  break;
case (>50):
  // Do this other stuff
  break;
}

You need an ifand else,

你需要一个ifand else,

if (age > 79) {
 // do this stuff.
} else if (age > 50) {
 // do this other stuff.
} // ...

回答by brso05

You can't do this use if then statement.

您不能使用 if then 语句执行此操作。

if(age > 79)
{
   //do stuff
}
else if(age > 50)
{
   //do stuff
}
else
{
   /do stuff
}

etc...

等等...

回答by shinjw

If you are using a loop you might want to look at What is the "continue" keyword and how does it work in Java?. This is not a good place to use switch.

如果您正在使用循环,您可能需要查看什么是“continue”关键字以及它在 Java 中是如何工作的?. 这不是使用 switch 的好地方。

if(age > 79)
{
   //do stuff
   continue; // STOP FLOW HERE AND CONTINUE THE LOOP
}
else if(age > 50)
{
   //do stuff
   continue; // STOP FLOW HERE AND CONTINUE THE LOOP
}

回答by NavinRaj Pandey

each case of switch is supposed to be an integer or String since JavaSE 7 and you are trying to feed a boolean value to it so its not possible .Read oracle doc to know about java switch in detail http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

自 JavaSE 7 以来,开关的每种情况都应该是一个整数或字符串,并且您正试图为其提供一个布尔值,因此不可能。阅读 oracle 文档以详细了解 java 开关 http://docs.oracle.com /javase/tutorial/java/nutsandbolts/switch.html

回答by NicePixel

You can't use conditional statements with switch.

您不能将条件语句与switch.

But you CAN do it with ifstatements! If you have a loop you can use continueto stop any upcoming lines and start from the beginning of the innermost loop.

但是你可以用if语句来做到这一点!如果你有一个循环,你可以continue用来停止任何即将到来的行并从最里面的循环的开头开始。

if(age>76){
    // Code...
    continue;
}else if(age>50){
    // More Code...
    continue;
}else{
    // Even more code...
    continue;
}

回答by Williem

It is not possible. Instead, Try this minimalist approach

这不可能。相反,尝试这种极简主义的方法

 age > 79 ? first_case_method() 
          : age > 50 ? second_case_method() 
          : age > 40 ? third_case_method() 
          : age > 30 ? fourth_case_method() 
          : age > 20 ? fifth_case_method()
          : ... 
          : default_case_method();

回答by Vikram

It can be done. The code needs to be slightly altered.

可以办到。代码需要稍微改动一下。

public static void main(String[]arguments)
    {
        int x=1, age=55;
        switch(x)
        {
        case 1: if((age>=60)&&(age<200))
            System.out.println("Senior Citizen");
        case 2: if((age>=18)&&(age<60))
            System.out.println("Adult");
        case 3: if((age>=12)&&(age<18))
            System.out.println("Teenager");
            break;
        default :
            System.out.println("Child");
            break;
        }
}