我希望在我决定退出之前重复我的菜单
I want my menu to be repeated until I decide to exit
目前,我的代码接收输入,并执行与数字(用户输入)匹配的代码,然后自动终止。但是,我希望我的菜单一直显示,直到用户决定退出(在我的例子中是数字 8)。
给定以下代码:
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 values\n"+
"Enter 2: Output the sum and mean of the values\n"+
"Enter 3: Output all odd numbers\n"+
"Enter 4: Output all even numbers\n"+
"Enter 5: Output Middle Value\n"+
"Enter 6: Output First value\n"+
"Enter 7: Output Last value\n"+
"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;
}
}
}
我希望我的菜单一直重复,直到我输入数字 8,所以我尝试通过替换 while(true) 循环内的 switch 语句来编辑我的代码,但输出显示如下:
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
固定码:
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 values\n"+
"Enter 2: Output the sum and mean of the values\n"+
"Enter 3: Output all odd numbers\n"+
"Enter 4: Output all even numbers\n"+
"Enter 5: Output Middle Value\n"+
"Enter 6: Output First value\n"+
"Enter 7: Output Last value\n"+
"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;
}
}
}
我该如何解决?
您可以删除 while(true)
循环,而不是在所有情况下设置 choice=true
,只需在 case 8
.
中将其设置为 true
你也可以使用do while循环,在while like choice is equal to exit中添加条件
int choice = -1;
do{
choice = scanner.nextInt();
// your logic
}while(choice != 8);
你有两个循环。
然后你在循环中创建了一个新的 Scanner 对象,这真的很乱。
您可以创建菜单功能,例如:
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
}
}
}
注意,如果需要在函数外使用scanner,需要先声明,然后作为函数的参数传递。 (你应该只有一个扫描仪实例)
目前,我的代码接收输入,并执行与数字(用户输入)匹配的代码,然后自动终止。但是,我希望我的菜单一直显示,直到用户决定退出(在我的例子中是数字 8)。
给定以下代码:
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 values\n"+
"Enter 2: Output the sum and mean of the values\n"+
"Enter 3: Output all odd numbers\n"+
"Enter 4: Output all even numbers\n"+
"Enter 5: Output Middle Value\n"+
"Enter 6: Output First value\n"+
"Enter 7: Output Last value\n"+
"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;
}
}
}
我希望我的菜单一直重复,直到我输入数字 8,所以我尝试通过替换 while(true) 循环内的 switch 语句来编辑我的代码,但输出显示如下:
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
固定码:
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 values\n"+
"Enter 2: Output the sum and mean of the values\n"+
"Enter 3: Output all odd numbers\n"+
"Enter 4: Output all even numbers\n"+
"Enter 5: Output Middle Value\n"+
"Enter 6: Output First value\n"+
"Enter 7: Output Last value\n"+
"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;
}
}
}
我该如何解决?
您可以删除 while(true)
循环,而不是在所有情况下设置 choice=true
,只需在 case 8
.
你也可以使用do while循环,在while like choice is equal to exit中添加条件
int choice = -1;
do{
choice = scanner.nextInt();
// your logic
}while(choice != 8);
你有两个循环。
然后你在循环中创建了一个新的 Scanner 对象,这真的很乱。
您可以创建菜单功能,例如:
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
}
}
}
注意,如果需要在函数外使用scanner,需要先声明,然后作为函数的参数传递。 (你应该只有一个扫描仪实例)