java - 执行方法后,如何返回到菜单?

  显示原文与译文双语对照的内容

package javaapplication10;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
/**
 *
 * @author Tom
 */
public class JavaApplication10 {
static int randomWithRange(int min, int max)
{
 int range = (max - min) + 1; 
 return (int)(Math.random() * range) + min;
}
public static void menu(){
 System.out.println("MENU:");
 System.out.println("1.Questions");
 System.out.println("2.High Scores");
 System.out.println("3.Exit");
 }//creates the function"menu" with 3 different options

public static void main(String[] args) {
 Scanner scanner = new Scanner(System.in);
 System.out.print("Enter your name:");
 String userName = scanner.next();//stores the user's name as the variable userName
 System.out.println("What class are you in"+userName+"? (1, 2 or 3");
 int userClass = scanner.nextInt();

 menu();//outputs the function"menu"
 System.out.println("Please choose an option from the menu:");
 int menuChoice = scanner.nextInt();//stores the integer that the user has entered as the menuChoice


 int[] highScores1 = new int[30];
 int[] highScores2 = new int[30];
 int[] highScores3 = new int[30];
 String[] Names = new String[30];
 boolean run = true;

 ArrayList calc = new ArrayList();//stores all the operations in an array list
 calc.add("+");
 calc.add("-");
 calc.add("*");

 while(menuChoice!= 3){
 switch(menuChoice){
 case 1://if the menuChoice = 1 it carries out te following code

 System.out.println("");
 System.out.println("Please answer the following questions:");
 System.out.println("This test will involve some negative answers so watch out for them!");
 System.out.println("");

 int counter = 0;//starts the counter at 0
 int counter1 = 0;
 int score = 0;

 Names[counter1] = userName;

 for (int i = 1; i<11; i++){
 int randomnr = randomWithRange(10,0);//creates a random number with the max number being 10
 int randomnr2 = randomWithRange(10,0);
 int randomcalc = randomWithRange(0,2);
 System.out.println("Question"+i+":"+randomnr+""+calc.get(randomcalc)+""+randomnr2);
 System.out.print("Enter the correct answer:");
 int answer = scanner.nextInt();


 if (randomcalc == 0){
 int calculation = randomnr+randomnr2;
 if(answer == calculation){
 System.out.println("Correct!");
 counter++;
 score++;
 System.out.println("");
 }
 else{
 System.out.println("Wrong,"+calculation+" is the correct answer");
 System.out.println("");
 }
 }
 if (randomcalc == 1){
 int calculation = randomnr-randomnr2;
 if(answer == calculation){
 System.out.println("Correct!");
 counter++;
 score++;
 System.out.println("");
 }
 else{
 System.out.println("Wrong,"+calculation+" is the correct answer");
 System.out.println("");
 }
 }
 if (randomcalc == 2){
 int calculation = randomnr*randomnr2;
 if(answer == calculation){
 System.out.println("Correct!");
 counter++;
 score++;
 System.out.println("");
 }
 else{
 System.out.println("Wrong,"+calculation+" is the correct answer");
 System.out.println("");

 }
 }
 }
 System.out.println("Congratulations"+userName+", you have scored"+counter+"/10");

 if(counter <= 3){
 System.out.println("You need to work harder!");
 }
 else if(counter <= 6 && counter> 3){
 System.out.println("Keep putting in the work and you will start getting those top grades!");
 }
 else if(counter <= 9 && counter> 8){
 System.out.println("Unlucky! With a little more revision you can achieve those top marks!");
 }
 else if(counter <= 0){
 System.out.println("Are you drunk?!");
 }
 else if(counter == 10){
 System.out.println("Well done"+userName+", your work has clearly paid off, top marks!");
 }

 highScores1[counter1] = counter;
 counter++;
 highScores2[counter1] = counter;
 counter++;
 highScores3[counter1] = counter;
 counter++;
 break;



 case 2:
 if(userClass == 1){
 System.out.println(Arrays.toString(Names));
 System.out.println(Arrays.toString(highScores1));
 }

 if(userClass == 2){
 System.out.println(Arrays.toString(Names));
 System.out.println(Arrays.toString(highScores1));
 }

 if(userClass == 3){
 System.out.println(Arrays.toString(Names));
 System.out.println(Arrays.toString(highScores3)); 
 }
 break;


 case 3:
 System.out.println("Please come back soon!");
 run = false;
 }






 }


}
}

我已经编写了这个代码,并且在执行代码之后还要返回菜单。 同样的例子 2,它连续打印数组,我想知道如何打印 array的内容一次,然后再次返回菜单。 感谢你的帮助。

时间: 作者:

你可以通过说"菜单( ) 来调用方法菜单,就像你使用任何其他方法一样。


if(x ==2){ 
 menu(); 
}

你需要重复 while() 循环中的菜单选择,只有在 switch() 语句结束之后和of循环结束之前。 添加:


menu();
System.out.println("Please choose an option from the menu:");
menuChoice = scanner.nextInt();

。在这一点上。

注意,这也意味着在 switch 语句中没有必要使用"案例 3"位- 只需将那些代码放在of循环之外。 如果将这些代码从 switch 语句重构为自己的方法,你会发现读取这些代码块更容易。

作者:
...