首页 > 解决方案 > 如何从方法退出回到菜单选择?

问题描述

我最近一直在做一些项目,我似乎无法完全破解如何从一个菜单选项进入一个方法,然后从那个方法返回到菜单并提示用户使用相同的选项?

String userChoice = console.nextLine();

        do {
            switch (userChoice) {

                case "1":
                    enteringContacts();
                    break;
                case:"xyz"
                   }
        }while(!userChoice.equals("xyz"));

我使用了 do While 循环,但它只是重复该方法?“EnteringContacts”方法如下所示:

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"));

    }

任何帮助都会得到帮助!

标签: javanetbeansmenu

解决方案


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

添加

userChoice = console.nextLine();

行前:

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

推荐阅读