如何在 Java 中创建嵌套菜单?
How to create a nested menu in Java?
我正在尝试创建一个菜单系统,用户可以在其中选择是管理员还是客户,然后他们可以在那里输入登录详细信息。
登录管理员后,客户会看到不同的菜单。我还希望能够注销,它将返回到登录菜单,询问他们是否有管理员或客户。
这是我目前拥有的:
import java.util.Scanner;
public class Main {
public static void main(String[] args){
login login = new login();
Scanner in = new Scanner(System.in);
String user;
String pwd;
int loginOption = 0;
login.databaseConnection();
System.out.println("======================================");
System.out.println("| Please select and option: |");
System.out.println("======================================");
System.out.println("| Options: |");
System.out.println("| [1] Admin |");
System.out.println("| [2] Account Holder |");
System.out.println("| [3] Exit |");
System.out.println("======================================");
do{
System.out.print("Option:");
loginOption = in.nextInt();
// Switch construct
switch (loginOption) {
case 1:
System.out.println("");
System.out.println("Username: ");
user = in.next();
System.out.println("Password: ");
pwd = in.next();
System.out.println("");
break;
case 2:
System.out.println("");
System.out.println("Account Number: ");
user = in.next();
System.out.println("Password: ");
pwd = in.next();
System.out.println("");
if(login.validate_login(user, pwd)){
System.out.println("Valid Login");
System.out.println("");
System.out.println("Hello, Account Holder");
System.out.println("");
System.out.println("======================================");
System.out.println("| Please select and option: |");
System.out.println("======================================");
System.out.println("| Options: |");
System.out.println("| [1] Check balence |");
System.out.println("| [2] Withdraw |");
System.out.println("| [3] Logout |");
System.out.println("======================================");
int userOption = 0;
do{
System.out.println("Option");
userOption = in.nextInt();
switch (userOption){
case 1:
break;
case 2:
break;
case 3:
loginOption = 3;
break;
}
}while (loginOption != 3);
}else{
System.out.println("Invalid");
}
break;
case 3:
System.out.println("Exit selected");
System.exit(0);
break;
default:
System.out.println("Invalid selection");
break; // This break is not really necessary
}
}while (loginOption != 3);
in.close();
}
}
我现在遇到的主要问题是离开第二个开关并返回到第一个。
任何帮助将不胜感激:)
您可以像这样添加布尔变量 ext
:
boolean ext = true;
do{
...
case 3:
loginOption = 3;
ext = false;
break;
}
}while (loginOption != 3 && ext);
当你有 loginOption = 3;
时,你可以退出 while 循环
我正在尝试创建一个菜单系统,用户可以在其中选择是管理员还是客户,然后他们可以在那里输入登录详细信息。
登录管理员后,客户会看到不同的菜单。我还希望能够注销,它将返回到登录菜单,询问他们是否有管理员或客户。
这是我目前拥有的:
import java.util.Scanner;
public class Main {
public static void main(String[] args){
login login = new login();
Scanner in = new Scanner(System.in);
String user;
String pwd;
int loginOption = 0;
login.databaseConnection();
System.out.println("======================================");
System.out.println("| Please select and option: |");
System.out.println("======================================");
System.out.println("| Options: |");
System.out.println("| [1] Admin |");
System.out.println("| [2] Account Holder |");
System.out.println("| [3] Exit |");
System.out.println("======================================");
do{
System.out.print("Option:");
loginOption = in.nextInt();
// Switch construct
switch (loginOption) {
case 1:
System.out.println("");
System.out.println("Username: ");
user = in.next();
System.out.println("Password: ");
pwd = in.next();
System.out.println("");
break;
case 2:
System.out.println("");
System.out.println("Account Number: ");
user = in.next();
System.out.println("Password: ");
pwd = in.next();
System.out.println("");
if(login.validate_login(user, pwd)){
System.out.println("Valid Login");
System.out.println("");
System.out.println("Hello, Account Holder");
System.out.println("");
System.out.println("======================================");
System.out.println("| Please select and option: |");
System.out.println("======================================");
System.out.println("| Options: |");
System.out.println("| [1] Check balence |");
System.out.println("| [2] Withdraw |");
System.out.println("| [3] Logout |");
System.out.println("======================================");
int userOption = 0;
do{
System.out.println("Option");
userOption = in.nextInt();
switch (userOption){
case 1:
break;
case 2:
break;
case 3:
loginOption = 3;
break;
}
}while (loginOption != 3);
}else{
System.out.println("Invalid");
}
break;
case 3:
System.out.println("Exit selected");
System.exit(0);
break;
default:
System.out.println("Invalid selection");
break; // This break is not really necessary
}
}while (loginOption != 3);
in.close();
}
}
我现在遇到的主要问题是离开第二个开关并返回到第一个。
任何帮助将不胜感激:)
您可以像这样添加布尔变量 ext
:
boolean ext = true;
do{
...
case 3:
loginOption = 3;
ext = false;
break;
}
}while (loginOption != 3 && ext);
当你有 loginOption = 3;