如何退出方法回到菜单选择?
How to exit from method back to a menu choice?
我最近一直在做几个项目,我似乎无法完全破解如何从一个菜单选项进入一个方法,然后从那个方法 return 到菜单并用相同的选项提示用户?
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"));
您应该尝试重新组织代码并将逻辑与用户对话框分开。您可以提供一个包含可能操作的枚举:
public enum UserAction {
EDIT_CONTACT,
ADD_CONTACT,
...,
QUIT
}
例如你的逻辑class:
class Logic {
public void mainLoop() {
while (true) {
UserAction action = UserDialogs.getUserAction();
switch(action) {
case EDIT_CONTACT:
editContact();
break;
case ...
case QUIT: return;
}
}
}
}
并且您应该将解析选项中输入的文本的责任转移到使用静态方法的专用class:
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"));
我最近一直在做几个项目,我似乎无法完全破解如何从一个菜单选项进入一个方法,然后从那个方法 return 到菜单并用相同的选项提示用户?
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"));
您应该尝试重新组织代码并将逻辑与用户对话框分开。您可以提供一个包含可能操作的枚举:
public enum UserAction {
EDIT_CONTACT,
ADD_CONTACT,
...,
QUIT
}
例如你的逻辑class:
class Logic {
public void mainLoop() {
while (true) {
UserAction action = UserDialogs.getUserAction();
switch(action) {
case EDIT_CONTACT:
editContact();
break;
case ...
case QUIT: return;
}
}
}
}
并且您应该将解析选项中输入的文本的责任转移到使用静态方法的专用class:
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"));