用户点击字母“M”时如何返回主菜单?

时间:2017-12-24 02:20:14

标签: java

用户输入字母“M”后如何返回主菜单。它会将它们引导到主菜单页面?

我尝试使用while(!inChoice.equalsIgnoreCase(“M”));但它不起作用。知道出了什么问题吗?

public static void searchByCountry(){

Range

String input = JOptionPane.showInputDialog(null,“Enter country number \ n”+ msg);

    int n, i, j, x;

    n = (uniqueCountries.length)-1;

    for(i=0; i<n; i++) {

        for(j=i+1; j<n;) {

            if(uniqueCountries[i]==uniqueCountries[j]) {

                for(x=j; x<n; x++) {

                    uniqueCountries[x] = uniqueCountries[x+1];
                } 

                n--;

            } 

            else {

                j++;
            }
        }
    }

    String msg = "";
    msg = "";

    for(i=0; i<n; i++) {

        String allUniqueCountries = uniqueCountries[i];

        msg += (i+1) + ". " + allUniqueCountries +" "+ "\n";       
    }

否则if(countryChoice == 3){

    int countryChoice = Integer.parseInt(input);

    String result = "";

    int count = 1;

    if(countryChoice == 1) {



        for(i=0; i<3; i++) {



        JOptionPane.showInputDialog(null, "Search result: " + count + "\n" 
        + "=======================\n" 
        + "Country: " + country[i] + "\n" 
        + "Month: " + travelmonth[i] + "\n" 
        + "Description: " + description[i] + "\n" 
        + "Price: $" + price[i] + "\n" + "=======================\n" 
        + "Enter M to return to main menu");
        count++;         

        }

    }

    //else if
    if (countryChoice == 2) {

        for(i=3; i<5; i++) {

        JOptionPane.showInputDialog(null, "Search result: " + count + "\n" 
        + "=======================\n" 
        + "Country: " + country[i] + "\n" 
        + "Month: " + travelmonth[i] + "\n" 
        + "Description: " + description[i] + "\n" 
        + "Price: $" + price[i] + "\n" 
        + "=======================\n" 
        + "Enter M to return to main menu");
        count++;

        }

    } 

}

1 个答案:

答案 0 :(得分:0)

您实际上需要接受新输入,然后检查它是否与“M”匹配。您提出问题的方式很难说明您实际上是如何管理输入的。它使用Scanner来获取用户输入。

Scanner sc = new Scanner(System.in);
while (true) {
    String line = sc.nextLine();
    if (line.equalsIgnoreCase("M")) {
        return;
    }
}

此外,在像这样的功能方法中进行轮询是不好的做法。您可以调用一个方法,例如waitForM(),它专门用于在您的主体中调用searchByCountry()方法后执行此操作。像这样的东西可以处理得更好。

    /* Scanner used to read input from console */
    public static Scanner sc = new Scanner(System.in);

    /* Main entry point to program */
    public static void main (String args[]) throws Exception {
        String input = "";
        /* Keep getting input until it is "exit", then finish */
        while (!(input = getInput()).equalsIgnoreCase("exit")) {
            System.out.println("Enter desired input. Type exit to exit");
            if (input.equalsIgnoreCase("search")) {
                SearchByCountry();
            }
            WaitForM();
        }
    }

    public static void SearchByCountry() {
        /* Do Stuff */
    }

    /* Method to wait for M Input */
    public static void WaitForM() {
        System.out.println("Enter M to return to Menu");
        String input = "";
        while (!(input = getInput()).equalsIgnoreCase("M")) {
            return;
        }
    }