For now, my code receives an input, and executes the code that matches the number(user input), and terminates automatically. However, I want my menu to keep showing up until the user decides to exit(number 8 in my case).
Given the following code:
int[] main_array;
main_array = new int[500];
boolean choice = false;
int menu = 0;
Random randomvar = new Random(); //creating random class object
for(int i = 0; i < 500; i++){
main_array[i] = randomvar.nextInt(1000) + 1;
}
while(!choice){
while(true){
Scanner sc = new Scanner(System.in);
System.out.println( "Enter 1: Output all valuesn"+
"Enter 2: Output the sum and mean of the valuesn"+
"Enter 3: Output all odd numbersn"+
"Enter 4: Output all even numbersn"+
"Enter 5: Output Middle Valuen"+
"Enter 6: Output First valuen"+
"Enter 7: Output Last valuen"+
"Enter 8: Exit");
try{
menu = sc.nextInt();
break;
} catch(Exception e) {
System.out.println("Wrong input");
}
}
switch(menu){
case 1:
choice = true;
for(int i = 0; i < 500; i++){
System.out.print(main_array[i] + " ");
}
break;
case 2:
choice = true;
int sum = 0;
for(int i = 0; i < 500; i++){
sum += main_array[i];
}
System.out.println("sum: " + sum);
System.out.println("Mean average: " + (sum / 500));
break;
case 3:
choice = true;
for(int i = 0; i < 500; i++){
if(main_array[i] % 2 == 1)
System.out.print(main_array[i] + " ");
}
break;
case 4:
choice = true;
for(int i = 0; i < 500; i++){
if(main_array[i] % 2 == 0)
System.out.print(main_array[i] + " ");
}
break;
case 5:
choice = true;
System.out.println(main_array[500/2]);
System.out.println(main_array[500/2]-1);
break;
case 6:
choice = true;
System.out.println(main_array[0]);
break;
case 7:
choice = true;
System.out.println(main_array[500 - 1]);
break;
case 8:
System.out.println("Exit the program");
return;
default:
System.out.println("Invalid input!");
break;
}
}
}
I want my menu to be keep repeated until I input the number 8, so I tried to edit my code by replacing the switch-statement inside the while(true) loop, but the output shows like this:
Enter 1: Output all values Enter 2: Output the sum and mean of the values Enter 3: Output all odd numbers Enter 4: Output all even numbers Enter 5: Output Middle Value Enter 6: Output First value Enter 7: Output Last value Enter 8: Exit 2 Enter 1: Output all values Enter 2: Output the sum and mean of the values Enter 3: Output all odd numbers Enter 4: Output all even numbers Enter 5: Output Middle Value Enter 6: Output First value Enter 7: Output Last value Enter 8: Exit 5 Enter 1: Output all values Enter 2: Output the sum and mean of the values Enter 3: Output all odd numbers Enter 4: Output all even numbers Enter 5: Output Middle Value Enter 6: Output First value Enter 7: Output Last value Enter 8: Exit
Fixed code:
int[] main_array;
main_array = new int[500];
boolean choice = false;
int menu = 0;
Random randomvar = new Random(); //creating random class object
for(int i = 0; i < 500; i++){
main_array[i] = randomvar.nextInt(1000) + 1;
}
while(!choice){
while(true){
Scanner sc = new Scanner(System.in);
System.out.println( "Enter 1: Output all valuesn"+
"Enter 2: Output the sum and mean of the valuesn"+
"Enter 3: Output all odd numbersn"+
"Enter 4: Output all even numbersn"+
"Enter 5: Output Middle Valuen"+
"Enter 6: Output First valuen"+
"Enter 7: Output Last valuen"+
"Enter 8: Exit");
try{
menu = sc.nextInt();
break;
} catch(Exception e) {
System.out.println("Wrong input");
}
switch(menu){
case 1:
choice = true;
for(int i = 0; i < 500; i++){
System.out.print(main_array[i] + " ");
}
break;
case 2:
choice = true;
int sum = 0;
for(int i = 0; i < 500; i++){
sum += main_array[i];
}
System.out.println("sum: " + sum);
System.out.println("Mean average: " + (sum / 500));
break;
case 3:
choice = true;
for(int i = 0; i < 500; i++){
if(main_array[i] % 2 == 1)
System.out.print(main_array[i] + " ");
}
break;
case 4:
choice = true;
for(int i = 0; i < 500; i++){
if(main_array[i] % 2 == 0)
System.out.print(main_array[i] + " ");
}
break;
case 5:
choice = true;
System.out.println(main_array[500/2]);
System.out.println(main_array[500/2]-1);
break;
case 6:
choice = true;
System.out.println(main_array[0]);
break;
case 7:
choice = true;
System.out.println(main_array[500 - 1]);
break;
case 8:
System.out.println("Exit the program");
return;
default:
System.out.println("Invalid input!");
break;
}
}
}
How should I fix it?
Advertisement
Answer
You have two loops.
And you create a new Scanner object in your loop, which is really messy.
You can create a menu function, for example :
private static void menu() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("menu");
switch (scanner.nextInt()) {
case 1:
// foo
break;
// and so on until case 8 :
case 8:
scanner.close();
return; // Leave your function. You don't even need to use a boolean
}
}
}
Note that if you need to use the scanner outside of the function, declare it before and pass it as a parameter of the function. (You should have only one instance of scanner)