public static void main(String[] args) { Scanner keyboard = new Scanner(System.i
ID: 3566315 • Letter: P
Question
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int choice = 0;
choice = printListChoice(keyboard);
processChoice(choice, keyboard);
}
public static int printListChoice(Scanner keyboard)
{
System.out.println("Please choose one of the following options "
+ "for your math quiz:");
System.out.println("1. Addition with numbers 1-10");
System.out.println("2. Addition with numbers 1-100");
System.out.println("3. Subtraction with numbers 1-10");
System.out.println("4. Subtraction with numbers 1-100");
System.out.println("5. Multiplication with numbers 1-10");
System.out.println("6. Exit the program");
int choice = 0;
System.out.println(" Enter your choice:");
choice = keyboard.nextInt();
return choice;
}
public static void processChoice(int choice, Scanner keyboard)
{
boolean run = true;
while (run){
if (choice == 1)
addNumOneToTen(keyboard);
else if (choice == 2)
addNumOneToHundred(keyboard);
else if (choice == 3)
subNumOneToTen(keyboard);
else if (choice == 4)
subNumOneToHundred(keyboard);
else if (choice == 5)
multiplyNum(keyboard);
else if (choice == 6)
exitProgram(keyboard);
else
System.out.println("Not a valid choice, redo.");
}
}
how would i make this code loop so i can go back and choose another number to do another quiz. i already have the code for everything else but i cannot figure out how to make it loop. Please help.
Explanation / Answer
// use the While Loop so that until user Enter the Choice 6 ( Exit ) then in process() we have another method to Exit the Progrmme
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int choice = 0;
while(true)
{
choice = printListChoice(keyboard);
// call to Process() in the process() we have exit code if the choice is 6
processChoice(choice, keyboard);
}
}
public static int printListChoice(Scanner keyboard)
{
System.out.println("Please choose one of the following options "
+ "for your math quiz:");
System.out.println("1. Addition with numbers 1-10");
System.out.println("2. Addition with numbers 1-100");
System.out.println("3. Subtraction with numbers 1-10");
System.out.println("4. Subtraction with numbers 1-100");
System.out.println("5. Multiplication with numbers 1-10");
System.out.println("6. Exit the program");
int choice = 0;
System.out.println(" Enter your choice:");
choice = keyboard.nextInt();
return choice;
}
public static void processChoice(int choice, Scanner keyboard)
{
boolean run = true;
while (run){
if (choice == 1)
addNumOneToTen(keyboard);
else if (choice == 2)
addNumOneToHundred(keyboard);
else if (choice == 3)
subNumOneToTen(keyboard);
else if (choice == 4)
subNumOneToHundred(keyboard);
else if (choice == 5)
multiplyNum(keyboard);
else if (choice == 6)
exitProgram(keyboard);
else
System.out.println("Not a valid choice, redo.");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.