views:

198

answers:

5

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.

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

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 (case 1);  // I don't know how to refference this correctly
        answer = x + y;
        TextIO.putln( x 'plus' y 'equals' answer);

Thanks in advance!

+1  A: 

Here's the presudo code for a rewrite of a CASE statement:

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.

Gert G
Looks like your first `if` has a `===` refugee from JavaScript
barrowc
@barrowc - Oops... that was a typo. Fixed. Thanks. :)
Gert G
A: 

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:

if(booleanExpression)
{

}
else if(booleanExpression2)
{

}
Oded
If you're using a straight up if statement, how are you handling this requirement: "ask a user for a number indicating the four basic math equations"?
Chris
I am not sure that asking the user these questions is directly linked to the requirement to use if statements for solving the equations.
Oded
I was making this way to hard and I don't have to use the switch statement. Just make a list of choices. I couldn't see the forest for all the trees.Thanks everyone!!!
jjason89
A: 

The switch is 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-else structure instead.

Sarfraz
+1  A: 

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.

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; 

}

eladidan
This is how I started, but because she wanted a letter selection to choose the operation and if/else for the operations I thought I would use a Case statment to get user input. 1 for add, 2 for subtract and so on. Then if I could use IF 'case =1' then do the following math operation.???
jjason89
A: 

You're switching on userSelection so userSelection already has the value you need; you can reference the value by referencing the userSelection variable:

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