简体   繁体   English

如何结合switch和if else语句

[英]how to combine switch and if else statements

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). 我正在上一个在线java课,老师要求以下内容:写一个菜单程序,询问用户一个数字,表示四个基本数学方程(加法,减法,乘法,除法)。 Using a if/else structure to do the required operation, ask the user for inputs and solve the equation. 使用if / else结构执行所需操作,询问用户输入并求解方程式。 I am new to this, but I don't see the point of the if/else, but I guess that is irrelavent. 我是新手,但我没有看到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! 提前致谢!

Here's the presudo code for a rewrite of a CASE statement: 这是重写CASE语句的presudo代码:

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. 你必须用Java编写代码。 :) Good luck with your assignment. :)祝你好运。

switch用于针对单个给定的变量/表达式,然后使用它的值,但由于您没有测试单个变量/表达式,这不适用,您应该使用if-elseif-else结构。

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. switch case语句是使用嵌套if..else语句的结构替代方法,您希望为数值或数值数据类型的不同值运行不同的逻辑。 It does not combine with if..else and the 2 are different approaches to solve conditional problems. 它不与if..else结合,而2是解决条件问题的不同方法。 Which one is better suited depends on the condition you are evaluating. 哪一个更适合取决于您正在评估的条件。

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; 

} }

You're switching on userSelection so userSelection already has the value you need; 您正在切换userSelection,因此userSelection已经具有您需要的值; you can reference the value by referencing the userSelection variable: 您可以通过引用userSelection变量来引用该值:

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

It would appear to me that your instructor does not want you to use the switch statement to begin with, but simply use the if flow control structure: 在我看来,你的教师不希望你开始使用switch语句,而只是使用if流控制结构:

if(booleanExpression)
{

}
else if(booleanExpression2)
{

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM