gpt4 book ai didi

java - 如何在Java中创建嵌套菜单?

转载 作者:行者123 更新时间:2023-12-01 10:30:29 25 4
gpt4 key购买 nike

我正在尝试创建一个菜单系统,用户可以在其中选择是管理员还是客户,然后他们可以在其中输入登录详细信息。

登录管理员后,客户会看到不同的菜单。我还希望能够注销,然后它将返回到登录菜单,询问他们是否有管理员或客户。

这是我到目前为止所拥有的:

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();
}
}

我现在遇到的主要问题是离开第二个开关并返回到第一个开关。

如有任何帮助,我们将不胜感激:)

最佳答案

您可以添加一个 boolean 变量ext,如下所示:

                      boolean ext = true;
do{
...
case 3:
loginOption = 3;
ext = false;
break;
}
}while (loginOption != 3 && ext);

这样,当您有 loginOption = 3;

时,您就可以退出 while 循环

关于java - 如何在Java中创建嵌套菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35116984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com