java 如何组合 switch 和 if else 语句

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

how to combine switch and if else statements

javaswitch-statementflow-control

提问by jjason89

I'm taking an online java class and the teacher has asked for the following: Write a menu program that ask a user for a number indicating the four basic math equations(addition, subtraction, multiplication, division). Using a if/else structure to do the required operation, ask the user for inputs and solve the equation. I am new to this, but I don't see the point of the if/else, but I guess that is irrelavent.

我正在参加在线 Java 课程,老师要求以下内容:编写一个菜单程序,要求用户输入一个数字,该数字表示四个基本数学方程(加、减、乘、除)。使用 if/else 结构执行所需的操作,询问用户输入并求解方程。我对此很陌生,但我看不到 if/else 的意义,但我想这是无关紧要的。

If I use case 1, 2, 3, 4 as below to determine the operation, how do I refference the case with a 'If statement'?

如果我使用下面的案例 1、2、3、4 来确定操作,我如何使用“If 语句”来引用案例?

int userSelection;
double x;        
double y;        
double answer;

switch ( userSelection ) {
   case 1:
        TextIO.putln("Let's add! ");
        TextIO.putln();
        TextIO.putln("Enter the first number: ");
        x = TextIO.getlnDouble();
    TextIO.putln("Enter the second number: ");
        y = TextIO.getlnDouble();
        break;

//I thought I could use the 'If' like this

//我想我可以像这样使用'If'

        if (case 1);  // I don't know how to refference this correctly
        answer = x + y;
        TextIO.putln( x 'plus' y 'equals' answer);

Thanks in advance!

提前致谢!

采纳答案by Oded

It would appear to me that your instructor does not want you to use the switchstatement to begin with, but simply use the ifflow control structure:

在我看来,您的讲师不希望您使用该switch语句开始,而只是使用if流程控制结构:

if(booleanExpression)
{

}
else if(booleanExpression2)
{

}

回答by Gert Grenander

Here's the presudo code for a rewrite of a CASEstatement:

这是重写CASE语句的前置代码:

IF (Case==1) THEN
  *What to do when Case==1.*
ELSE IF (Case==2) THEN
  *What to do when Case==2.*
ELSE IF (Case==3) THEN
  *What to do when Case==3.*
ELSE IF (Case==4) THEN
  *What to do when Case==4.*
END IF

You'll have to code this in Java though. :) Good luck with your assignment.

不过,您必须用 Java 对此进行编码。:) 祝你的任务好运。

回答by Sarfraz

The switchis used against a single given variable/expression and then cases are used on what value it is but because you are not testing a single variable/expression, this is not applicable, you should use if-elseif-elsestructure instead.

Theswitch用于单个给定的变量/表达式,然后将 case 用于它是什么值,但因为您不是在测试单个变量/表达式,所以这不适用,您应该改用if-elseif-else结构。

回答by eladidan

The switch case statements is a structural alternative to using nested if..else statements where you want to run differnet logic for different values of a numerical or enumerical data type. It does not combine with if..else and the 2 are different approaches to solve conditional problems. Which one is better suited depends on the condition you are evaluating.

switch case 语句是使用嵌套 if..else 语句的结构替代方案,您希望在其中为数值或枚举数据类型的不同值运行不同的逻辑。它不与 if..else 结合,这两种是解决条件问题的不同方法。哪个更适合取决于您正在评估的条件。

If I understand your code correctly, then I think you are looking for something like this:

如果我正确理解您的代码,那么我认为您正在寻找这样的东西:

switch ( userSelection ) {  
case 1:  
    TextIO.putln("Let's add! ");  
    TextIO.putln();  
    TextIO.putln("Enter the first number: ");  
    x = TextIO.getlnDouble();  
    TextIO.putln("Enter the second number: ");  
    y = TextIO.getlnDouble();  
    answer = x + y;    
    TextIO.putln( x + " plus " + y + " equals " + answer);   
    break;  
case 2:  
    ... //oh, I don't know. but I would guess: let's subtract!  
    break; 
case 3:  
    ... //let's multiply!
    break; 
case 4:  
    ... //let's divide! 
    break; 

}

}

回答by Ian R. O'Brien

You're switching on userSelectionso userSelectionalready has the value you need; you can reference the value by referencing the userSelectionvariable:

您正在打开userSelection,因此userSelection已经具有您需要的值;您可以通过引用userSelection变量来引用该值:

if (userSelection == 1) {
    // add
} else if (userSelection == 2) {
   // subtract
} else if (userSelection == 3) {
   // multiply
} else if (userSelection == 2) {
   // divide
}