class calculator{ public static void main(String[] args){ char option; System.out.print("Please choose one of the following:"); System.out.print("(A) Add"); System.out.print("(B) Subtract"); option=keyboard.readChar (); if (option=A){ add(); } else (option=B) { subtract(); } static void add(){ int number1, number2; System.out.println("Please enter two numbers to add: "); number1 = Keyboard.readInt(); number2 = Keyboard.readInt(); System.out.println("Your answer is:"); System.out.println(number1 + number2); } } static void subtract(){ int number3, number4, System.out.println("Please enter two numbers to subtract: "); number3 = Keyboard.readInt(); number4 = Keyboard.readInt(); System.out.println("Your answer is:"); System.out.println(number3 - number4); } }
int number3, number4,
class calculator{ public static void main(String[] args){ char option; System.out.print("Please choose one of the following:"); System.out.print("(A) Add"); System.out.print("(B) Subtract"); option=keyboard.readChar (); switch (option) { case "A": add(); case "B" subtract(); } [b] but I'm pretty sure you want it to end here[/b] static void add(){ int number1, number2; System.out.println("Please enter two numbers to add: "); number1 = Keyboard.readInt(); number2 = Keyboard.readInt(); System.out.println("Your answer is:"); System.out.println(number1 + number2); } } [b] main is ending here[/b]
switch (option) { case "A": add(); case "B" subtract(); } [B]should be[/B] switch (option) { case 'A': add(); case 'B' subtract(); }
while(option != 4){ // while the input does not equal 4, then run the switch // switch case in here }
dlofnep wrote: » What you can do is use a while loop to take input prior to the switch statement. Use the same variable as the switch statement. So basically, you would have prior to the switch statement Otherwise, it will skip past the switch case and end the program That way, you will have the menu looping until they select the 4th option. I'd reccommend using numbers over letters for simplicity, as in java you need to use .equals to compare strings instead of == or != Best of luck.
dlofnep wrote: » Didn't I just say that?
do{ switch (option) { case 1: add(); break; case 2: subtract(); break; case 3: multiply(); break; case 4: quit(); break; default: //invalid option. Print to screen System.out.println("Invalid Choice"); } while (option < 1 && option > 3); }
do{ switch (option) { case 1: add(); break; case 2: subtract(); break; case 3: multiply(); break; case 4: quit(); break; default: //invalid option. Print to screen System.out.println("Invalid Choice"); } } while (option < 1 && option > 3);
Webmonkey wrote: » Not really no, plus <snip - i'm half asleep> do { //switch statement in here } while (option.charAt(0) != 'D'); You are right about numbers though, better choice. Then you can do a range and say wrong option.
D:\College\Java Programming\Java Programming\Assignment 2\calculator.java:40: while expected } ^
do{ System.out.println("Entered do. Option was: " + option ); switch (option) { case 1: System.out.println("Selected 1. Entering Add"); add(); System.out.println("Exiting Add"); break;
do{ //put your i/o here switch (option) { case 1: add(); break; case 2: subtract(); break; case 3: multiply(); break; case 4: quit(); break; default: //invalid option. Print to screen System.out.println("Invalid Choice"); } } while (option < 1 && option > 3);
Donny5 wrote: » do{ //put your i/o here switch (option) { case 1: add(); break; case 2: subtract(); break; case 3: multiply(); break; case 4: quit(); break; default: //invalid option. Print to screen System.out.println("Invalid Choice"); } } while (option < 1 && option > 3); Try (option >= 1 && option <= 3).