我想在无效选项后重复菜单?

时间:2014-07-08 03:02:34

标签: java

好的我知道这一定比我要做的更简单,但我似乎无法弄明白。我有菜单。用户选择一个调用某种方法的数字。

如果菜单没有选择有效选项,我希望重复菜单。然后我想给他们选择另一个选项或结束程序的选项。

任何人都可以帮助我吗?

import java.util.Scanner;


public class TestStringManip {
        //=================MAIN===============
    public static void main(String[] args) 
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String input = in.nextLine();

        StringManipulation newString = new StringManipulation(input);

        String menu = "\n Please select an option to perform"
                + "\n1 (1) Get all uppercase letters."
                + "\n2 (2) Get every second letter."
                + "\n3 (3) Replace vowels with _ "
                + "\n4 (4) Get a count of all vowels."
                + "\n5 (5) Get position of all vowels.";
        System.out.println(menu);
        int option = in.nextInt();


        do
        {
            if (option == 1)
            {
                newString.getUpperCase(input);
            }

            else if (option == 2)
            {
                newString.getEverySecond(input);
            }

            else
            {
                System.out.println("Error must select 1-5");
                System.out.println(menu);
                option = in.nextInt();
            }
        }while (option < 1 || option > 5);
    }

}

我已更新我的代码以尝试一些建议的答案。它适用于前两个选项,但就是这样。我也试图在选择每个选项后重复它,除非选择0结束程序。这是我更新的代码。

import java.util.Scanner;


public class TestStringManip {
        //=================MAIN===============
    public static void main(String[] args) 
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String input = in.nextLine();

        StringManipulation newString = new StringManipulation(input);

        String menu = "\n Please select an option to perform"
                + "\n1 (1) Get all uppercase letters."
                + "\n2 (2) Get every second letter."
                + "\n3 (3) Replace vowels with _ "
                + "\n4 (4) Get a count of all vowels."
                + "\n5 (5) Get position of all vowels."
                + "\n0 (0) Exit Program";
        System.out.println(menu);
        int option = in.nextInt();

        int i = 0;
        do
        {   
            if (option == 1)
            {
                newString.getUpperCase(input);
                i = 1;
            }

            else if (option == 2)
            {
                newString.getEverySecond(input);
                i = 1;
            }

            else if (option == 3)
            {
                newString.replaceVowels(input);
                i = 1;
            }

            else if (option == 4)
            {
                newString.getNumberOfVowels(input);
                i = 1;
            }

            else if (option == 5)
            {
                newString.getVowelPosition(input);
                i = 1;
            }

            else if (option == 0)
            {
                break;
            }

            else
            {
                System.out.println("Error must select 0-5");
                System.out.println("Enter your option: ");
                option = in.nextInt();
            }
        }while (i == 0);

    }

}

5 个答案:

答案 0 :(得分:1)

尝试使用switch语句。用这个代码块替换你的if语句。

do{
    switch(option){
        case 0:
             //Exit loop
             break;
        case 1:
             newString.getUpperCase(input);
             break;
        case 2:
             newString.getEverySecond(input);
             break;
        case 3:
             // Method number 3;
        . . .

        default:
            System.out.println("Please enter a number between 1 and 5");
            break;
    }
} while(option !=0)

修改 如果您想了解有关switch语句的更多信息,请查看此java tutorial

重新编辑:是的,你必须检查int输入而不是Ricky所说的字符串

答案 1 :(得分:1)

代码:

  public class TestStringManip {


public static void main(String[] args) {
    String menu = "\n Please select an option to perform"
            + "\n1 (1) for option one enter 1."
            + "\n2 (2) for option two enter 2."
            + "\n0 (0) Exit Program";
    Scanner in = new Scanner(System.in);
    int option = ReadAndMenu(menu,in);

    int i = 0;
    do {
        if (option == 1) {
            System.out.println("option one");
            option = ReadAndMenu(menu,in);
        } else if (option == 2) {
            System.out.println("option two");
            option = ReadAndMenu(menu,in);
        } else if (option == 0){
            System.out.println("exit");
            i= 1; 
        } else {
            System.out.println("Please enter a number between 1 and 2");
            option = ReadAndMenu(menu,in);

        }
    } while (i == 0);

}

 private static int ReadAndMenu(String menu, Scanner in){
        System.out.println(menu);
        System.out.print("Enter your option: ");
        return in.nextInt();
    }

}

输出:

Please select an option to perform
1 (1) for option one enter 1.
2 (2) for option two enter 2.
0 (0) Exit Program
Enter your option: 1
option one

 Please select an option to perform
1 (1) for option one enter 1.
2 (2) for option two enter 2.
0 (0) Exit Program
Enter your option: 2
option two

 Please select an option to perform
1 (1) for option one enter 1.
2 (2) for option two enter 2.
0 (0) Exit Program
Enter your option: 5
Please enter a number between 1 and 2

 Please select an option to perform
1 (1) for option one enter 1.
2 (2) for option two enter 2.
0 (0) Exit Program
Enter your option: 0

答案 2 :(得分:1)

我会向Scanner.hasNextInt()添加一个检查,以确保用户实际输入的是int。因为现在,如果有人输入“你好”,它会破坏你的程序。我修改了代码,希望循环。

这是一个例子,使用代码的修改版本:(对不起的缩进版本感到遗憾 - 你的版本很奇怪,我还没有修复它。会修复修改)

import java.util.Scanner;
public class TestStringManip {
    public static void main(String[] args) 
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String input = in.nextLine();

        StringManipulation newString = new StringManipulation(input);

        String menu = "\n Please select an option to perform"
                + "\n1 (1) Get all uppercase letters."
                + "\n2 (2) Get every second letter."
                + "\n3 (3) Replace vowels with _ "
                + "\n4 (4) Get a count of all vowels."
                + "\n5 (5) Get position of all vowels.";
        System.out.println(menu);

        int option = getIntInput(in);

        do{
            if (option == 1)
            {
                newString.getUpperCase(input);
            }

            else if (option == 2)
            {
                newString.getEverySecond(input);
            }

            else if (option == 0){
                System.out.println("Goodbye!");
                System.exit(0);
            }

            else
            {
                option = getIntInput(in);
            }

          }while(option != 0);
    }

  //this function will keep prompting until the user inputs a valid option
  int getIntInput(Scanner in){
     while(!in.hasNextInt()){
        System.out.println("Please enter a valid option (# between 1 and 5)");
     }

     return in.nextInt();
  }


}

答案 3 :(得分:1)

您希望逻辑工作的方式我相信应该有退出的选项。那么如何尝试以下内容,

do{
      String menu = "\n Please select an option to perform"
        + "\n1 (1) Get all uppercase letters."
        + "\n2 (2) Get every second letter."
        + "\n3 (3) Replace vowels with _ "
        + "\n4 (4) Get a count of all vowels."
        + "\n5 (5) Get position of all vowels."
        + "\n0 (0) To Exit.";
     System.out.println(menu);
     int option = in.nextInt();


    if (option == 1){
        newString.getUpperCase(input);
    }

    else if (option == 2){
        newString.getEverySecond(input);
    }
    ...
    ...
    else if(option == 0){
        break;
    }else{
        System.out.println("\n\nWrong Choice\n\n");
    }

}while(option != 0);

修改

因为Kick Buttowski有这么多疑惑。我创建了两个简单的示例,只需自己运行它们并查看输出,因为我没有足够的时间上传两个输出的屏幕截图。

MyCode 它可以正常使用

import java.util.Scanner;
class MyTest{
public static void main(String args[]){

    Scanner in = new Scanner(System.in);
 int option = 0;
    do{
  String menu = "\n Please select an option to perform"
    + "\n1 (1) Get all uppercase letters."
    + "\n2 (2) Get every second letter."
    + "\n3 (3) Replace vowels with _ "
    + "\n4 (4) Get a count of all vowels."
    + "\n5 (5) Get position of all vowels."
    + "\n0 (0) To Exit.";
 System.out.println(menu);

option = in.nextInt();



if (option == 1){
System.out.println("1");
}

else if (option == 2){
System.out.println("2");
}

else if(option == 0){
System.out.println("0");
    break;
}else{
    System.out.println("\n\nWrong Choice\n\n");
}

}while(option != 0);

}
}

为了澄清这里的区别,其他用户提出的代码将导致无限循环

import java.util.Scanner;
class MyTest2{
public static void main(String args[]){

    Scanner in = new Scanner(System.in);
 int option = 1;
    do{



if (option == 1){
System.out.println("1");
}

else if (option == 2){
System.out.println("2");
}

else if(option == 0){
System.out.println("0");
    break;
}else{
    System.out.println("\n\nWrong Choice\n\n");


  String menu = "\n Please select an option to perform"
    + "\n1 (1) Get all uppercase letters."
    + "\n2 (2) Get every second letter."
    + "\n3 (3) Replace vowels with _ "
    + "\n4 (4) Get a count of all vowels."
    + "\n5 (5) Get position of all vowels."
    + "\n0 (0) To Exit.";
 System.out.println(menu);

option = in.nextInt();



}

}while(option != 0);

}
}

答案 4 :(得分:1)

不确定这是否是您要执行的操作,请重复整个菜单,除非用户按0,然后退出。

我使用switch代替if-else。但是你可以改变那段代码。试试看,告诉我这是不是你想要的。

这个在交换机处理之前验证输入

import java.util.Scanner;

class MyTest {
    public static void main(String args[]) {
        int option;
        Scanner in = new Scanner(System.in);
        do {
            do {
                String menu = "\n Please select an option to perform"
                                + "\n1 (1) Get all uppercase letters."
                                + "\n2 (2) Get every second letter."
                                + "\n3 (3) Replace vowels with _ "
                                + "\n4 (4) Get a count of all vowels."
                                + "\n5 (5) Get position of all vowels."
                                + "\n0 (0) Exit Program";
                System.out.println(menu);
                option = in.nextInt();
            } while(option < 0 || option > 5); // This will make the menu repeat if option is higher than 6 or lowen than 0.

            switch(option) {
                case 1:
                    System.out.println("Option 1");
                    break;
                case 2:
                    System.out.println("Option 2");
                    break;
                case 3:
                    System.out.println("Option 3");
                    break;
                case 4:
                    System.out.println("Option 4");
                    break;
                case 5:
                    System.out.println("Option 5");
                    break;
                default:
                    System.out.println("Hasta la vista Baby");
                    break; //I always use this break, even when not needed.
            }
        } while (option != 0);
    }
}

修改

正如GPRathour指出的那样,你可以使用一个简单的do-while循环来实现它,所以它看起来像这样:

这个在交换机上处理它后验证输入

import java.util.Scanner;

class MyTest {
    public static void main(String args[]) {
        int option;
        Scanner in = new Scanner(System.in);
        do {
            String menu = "\n Please select an option to perform"
            + "\n1 (1) Get all uppercase letters."
            + "\n2 (2) Get every second letter."
            + "\n3 (3) Replace vowels with _ "
            + "\n4 (4) Get a count of all vowels."
            + "\n5 (5) Get position of all vowels."
            + "\n0 (0) Exit Program";
            System.out.println(menu);
            option = in.nextInt();
            switch(option) {
                case 0:
                    System.out.println("Hasta la vista Baby");
                    break;
                case 1:
                    System.out.println("Option 1");
                    break;
                case 2:
                    System.out.println("Option 2");
                    break;
                case 3:
                    System.out.println("Option 3");
                    break;
                case 4:
                    System.out.println("Option 4");
                    break;
                case 5:
                    System.out.println("Option 5");
                    break;
                default:
                    System.out.println("Wrong option");
                    break; //I always use this break, even when not needed.
            }
        } while (option != 0);
    }
}