Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 执行方法后如何返回菜单?_Java_Arrays_Loops_Menu - Fatal编程技术网

Java 执行方法后如何返回菜单?

Java 执行方法后如何返回菜单?,java,arrays,loops,menu,Java,Arrays,Loops,Menu,我已经编写了这段代码,在代码执行后,我正在努力返回菜单。同样在我的案例2中,它连续打印数组,我想知道如何打印一次数组的内容,然后再次返回菜单。谢谢你的帮助 您可以通过说出menu来调用method menu,就像使用任何其他方法一样 ifx==2{ 菜单 } 您需要在while循环中重复菜单选择,就在switch语句结束之后和while循环的右括号之前。添加: package javaapplication10; import java.util.ArrayList; import java.

我已经编写了这段代码,在代码执行后,我正在努力返回菜单。同样在我的案例2中,它连续打印数组,我想知道如何打印一次数组的内容,然后再次返回菜单。谢谢你的帮助

您可以通过说出menu来调用method menu,就像使用任何其他方法一样

ifx==2{ 菜单
} 您需要在while循环中重复菜单选择,就在switch语句结束之后和while循环的右括号之前。添加:

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;
    }






    }


}
}
。。。在这一点上,我们应该这样做


注意,这也意味着在switch语句中使用case 3位没有任何意义——只需将该位代码放在while循环之外即可。如果您将switch语句中的一些代码重构为自己的方法,您会发现阅读这些代码块会更容易。

谢谢,这一点现在看起来很明显,但我对编程还是相当陌生的!
menu();
System.out.println("Please choose an option from the menu:");
menuChoice = scanner.nextInt();