java - 如何从方法退出回到菜单选择?
问题描述
我最近一直在做一些项目,我似乎无法完全破解如何从一个菜单选项进入一个方法,然后从那个方法返回到菜单并提示用户使用相同的选项?
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"));
}
任何帮助都会得到帮助!
解决方案
因为在再次循环“while”之前,您应该在每个循环中再次询问用户他的选择。
添加
userChoice = console.nextLine();
行前:
}while(!userChoice.equals("xyz"));
推荐阅读
- c++ - 'void QAbstractButton::clicked(bool)' 受保护
- python - Postgres:新创建的用户无法使用数据库
- r - xts 将数据从 chr 转换为 numeric
- c++ - 当我尝试在 DllMain 中显示它时,我的对话框不显示
- java - 从 Oracle 函数调用 Java 函数
- c# - 当鼠标悬停在窗口上时,如何更改窗口的背景?
- c# - 在 Visual Studio 中设置但尚未绑定的断点
- python-3.x - matplotlib ylabel TypeError:“str”对象不可调用
- git - 获取 NPM 项目的最新 Git 标签
- c++ - C++ 打印与死亡钻石