Issue
I have created a code for a menu system for my coin counter, I'm trying to make it so the user can choose more than one option after they have chosen the first until they decide to end the program. It doesn't loop and the code only runs one option then I have to restart the code manually via run on eclipse. Anyone could give me some insight into what I have missed here, thank you:)
Scanner ss=new Scanner(System.in);
int Choice;
int opt;
System.out.println("***Coin Sorter - Main Menu***");
System.out.println(" 1 - Coin Calculator");
System.out.println(" 2 - Multiple coin calculator");
System.out.println(" 3 - Print Coin list");
System.out.println(" 4 - Set details");
System.out.println(" 5 - Display program configurations");
System.out.println(" 6 - Quit the program");
Choice=ss.nextInt();
switch(Choice) {
case 1: coinCalculator();
break;
case 2: multiCoinCalculator();
break;
case 3: printCoinList();
break;
case 4: System.out.println("***Set Details Sub-Menu***");
System.out.println("1 - Set currency");
System.out.println("2 - Set minimum coin input value");
System.out.println("3 - Set maximum coin input value");
System.out.println("4 - Return to main menu");
opt=ss.nextInt();
if (opt==1) {
setCurrency(Currency);
}else if (opt==2) {
setMinCoinin(opt);
}else if (opt==3) {
setMaxCoinin(opt);
}else if (opt==4);
CoinSorter();
break;
case 5: displayProgramConfigs();
break;
case 6: if(Choice != 6) System.out.println("Unkown option");
} while (Choice !=6);
Solution
- Use
default
forUnkown option
. Learn more about it from the tutorial, The switch Statement - Put the menu inside a
do-while
loop properly. You have misseddo
in your code. - You have wrongly put a
;
afterelse if (opt==4)
Last but not least, always follow the Java naming conventions e.g. the name of the variable can be choice
but should not be Choice
.
Demo:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner ss = new Scanner(System.in);
int choice;
int opt;
do {
System.out.println("***Coin Sorter - Main Menu***");
System.out.println(" 1 - Coin Calculator");
System.out.println(" 2 - Multiple coin calculator");
System.out.println(" 3 - Print Coin list");
System.out.println(" 4 - Set details");
System.out.println(" 5 - Display program configurations");
System.out.println(" 6 - Quit the program");
choice = ss.nextInt();
switch (choice) {
case 1:
coinCalculator();
break;
case 2:
multiCoinCalculator();
break;
case 3:
printCoinList();
break;
case 4:
System.out.println("***Set Details Sub-Menu***");
System.out.println("1 - Set currency");
System.out.println("2 - Set minimum coin input value");
System.out.println("3 - Set maximum coin input value");
System.out.println("4 - Return to main menu");
opt = ss.nextInt();
if (opt == 1) {
setCurrency(Currency);
} else if (opt == 2) {
setMinCoinin(opt);
} else if (opt == 3) {
setMaxCoinin(opt);
} else if (opt == 4) {
CoinSorter();
}
break;
case 5:
displayProgramConfigs();
break;
case 6:
System.out.println("Good Bye!");
break;
default:
System.out.println("Unkown option");
}
} while (choice != 6);
}
}
Answered By - Arvind Kumar Avinash