Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/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_Netbeans_Menu - Fatal编程技术网

Java 如何退出方法返回菜单选项?

Java 如何退出方法返回菜单选项?,java,netbeans,menu,Java,Netbeans,Menu,我最近一直在做一些项目,我似乎无法完全理解如何从一个菜单选项进入一个方法,但是从该方法返回菜单并用相同的选项提示用户 String userChoice = console.nextLine(); do { switch (userChoice) { case "1": enteringContacts(); break;

我最近一直在做一些项目,我似乎无法完全理解如何从一个菜单选项进入一个方法,但是从该方法返回菜单并用相同的选项提示用户

String userChoice = console.nextLine();

        do {
            switch (userChoice) {

                case "1":
                    enteringContacts();
                    break;
                case:"xyz"
                   }
        }while(!userChoice.equals("xyz"));
我使用了do While循环,但它只是重复这个方法?“输入联系人”的方法如下所示:

public static void enteringContacts(){

        Scanner console = new Scanner(System.in);

        System.out.println("Welcome, How to use the system:");
        System.out.println("Enter the contacts name, then press enter");
        System.out.println("Enter the contacts number, then press enter");

        String exitContactInsert = " ";

            do {
                    contactName.add(console.nextLine());
                contactNumber.add(console.nextLine());

                System.out.println("Do you wish to add another?");
                exitContactInsert = console.nextLine();

                if (exitContactInsert.equals("no")) {
                    return;
                } else {
                    System.out.println("Please enter another contact");
                }
            } while (exitContactInsert.equals("yes"));

    }

感谢您的帮助

因为在再次循环“while”之前,您应该在每个循环中再次询问用户的选择

行前:

}while(!userChoice.equals("xyz"));

您应该尝试重新组织代码,并将逻辑与用户对话框分开。您可以为枚举提供可能的操作:

public enum UserAction {
    EDIT_CONTACT,
    ADD_CONTACT,
    ...,
    QUIT
}
例如,您的逻辑类:

class Logic {
    public void mainLoop() {
        while (true) {
            UserAction action = UserDialogs.getUserAction();
            switch(action) {
                case EDIT_CONTACT: 
                    editContact();
                    break;
                case ...
                case QUIT: return;
            }
        }
    }
}
您应该将解析选项中输入的文本的责任转移到使用静态方法的专用类:

class UserDialogs {
    public static UserAction getUserAction() {
        System.out.println("Select an action (1-edit, 2-add, 3-..., Q-quit):");
        Scanner scanner = new Scanner(System.in);
        while (true) {
            String response = scanner.nextLine().toUpperCase();
            switch (response) {
                case "1" : return EDIT_CONTACT;
                case "2" : return ADD_CONTACT;
                case ...
                case "Q" : return QUIT;
                default: 
                    System.out.println("Wrong selection, try again");
            }
        }
    }
}

如果您将在循环中获取用户响应,那么每个迭代将分别确定,每次执行不同的操作,直到用户提供xyz为止

String userChoice;
        do {
            userChoice = console.nextLine();
            switch (userChoice) {                
                case "1":
                    enteringContacts();
                    break;
                case:"xyz"
            }
        }while(!userChoice.equals("xyz"));
String userChoice;
        do {
            userChoice = console.nextLine();
            switch (userChoice) {                
                case "1":
                    enteringContacts();
                    break;
                case:"xyz"
            }
        }while(!userChoice.equals("xyz"));