jeudi 19 février 2015

Menu selection with if else and switch

Hi I am programming a calculator with the 5 choices of operations.



1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit


I want to ask the user to make a choice of the operation and check whether the choice valid (i.e. 1 - 5) If not, give a wrong message and prompt the user to select again.


I am thinking using if-else statement with switch statement within the else statement.


System.out.printf("What would you like to do? "); int selection = input.nextInt();


if (selection!=1 || 2 || 3 || 4 || 5) {



System.out.println("You have entered an invalid choice, please re-enter
your choice: ");
}/*end if as long as the selection is NOT a 1 - 5, prompt the user to
re-enter*/

else {
switch(selection){

case 1:
case 2:
case 3:
case 4:
case 5;


I am getting an Eclipse compiler error at the if line: "The operator || is undefined for the argument type(s) boolean, int"


Any ideas what is wrong and how to fix this? Thanks


Kelvin


Aucun commentaire:

Enregistrer un commentaire