gpt4 book ai didi

java - 用户选择时使用 switch case 和 while 循环进行无限循环

转载 作者:行者123 更新时间:2023-11-30 06:53:11 26 4
gpt4 key购买 nike

我正在尝试使用用户输入创建一个 CLI,以便从可用选项中进行选择。我使用 switchwhile 循环来确保它可以在选择选项后返回主菜单。问题是,用户成功登录后,会转到情况 4,但它会(无限)重复情况 4,我不知道为什么。

这是代码:

public class MainApplication {

private static User userLoggedIn;
private static Device deviceLoggedIn;
private static PrivateKey privateKey;
private static AbePrivateKey secretKey;

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// CpabeDAO.em.getTransaction().begin();

System.out.println("Welcome to CPABE Cloud");

int test = 0;
while (true){
switch(test){
case 0: //Greeting for the user. User now chooses the option.
System.out.println("Please choose the option by typing the option number:");
System.out.println("(1) Login");
System.out.println("(2) Register User");
System.out.println("(3) Exit");
int input = scanner.nextInt();
if(input < 0 || input > 3){
System.out.println("Unrecognized input.");
test = 0;
}
else {
test = input;
}
break;
case 1:
System.out.println("Login");
List<User> userRetrieve = login();
//check if it is successfully logged in
boolean loggedIn = (userRetrieve.size() == 0);
if(!loggedIn) {
for(User user : userRetrieve) {
userLoggedIn = user;
}
//Retrieve private key & secret key from cpabe dir, if it is not stored then register new device. After that go to case 4, which is the main menu;
test = 4;
}
else{
System.out.println("Your username or password is incorrect. Please choose what to do next:");
System.out.println("(1) Login");
System.out.println("(2) Back to main menu");
int input2 = scanner.nextInt();
if(input2 == 1){
test = 1;
}
else if(input2 == 2){
test = 0;
}
}
break;
case 2:
System.out.println("Register User");

userLoggedIn = UserRegistration.registerUser();
DeviceRegistration.registerNewDeviceforNewUser(userLoggedIn);

test = scanner.nextInt();
break;
case 3:
System.out.println("Exit");
System.out.println("Press 0 to go back to menu");
test = scanner.nextInt();
break;
case 4: //the main menu. user can have full functionalities after logging in or registering.
System.out.println("Welcome " + userLoggedIn.getFirstname());
System.out.println("Please select the option by typing the option number:");
break;
default :
System.out.println("Invalid input. Please insert the correct input.");
test = 0;
}
}
}

private static List<User> login(){
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter your username:");
String username = scanner.nextLine();
System.out.println("Please enter your password:");
String password = scanner.nextLine();
String hashPassword = Encryption.generateHash(password.getBytes());
Query q = CpabeDAO.em.createQuery("SELECT u FROM User u WHERE u.username = '" + username + "' and u.hashPassword = '" + hashPassword + "'");
List<User> userRetrieve = q.getResultList();
scanner.close();
return userRetrieve;
}

我想要实现的是,用户成功登录后,系统将转到情况4,即主菜单,并等待用户的输入,但它不断重复情况4。

最佳答案

正如@Abubakkar所说,你无法从开关中打破循环。此外,您仍处于第四种情况,因为您从未更改 test 值,也从未向扫描仪请求另一个值,因此它保持为 4。如果您想返回菜单,您应该直接从开关将其设置回 0。你可以这样做:

    boolean firstMenu = true;
boolean secondmenu = false;
while(firstMenu){
[...]
case 4: //the main menu. user can have full functionalities after logging in or registering.
System.out.println("Welcome " + userLoggedIn.getFirstname());
System.out.println("Please select the option by typing the option number:");
firstMenu = false;
secondMenu = true;
break;
}
[...]
}
while(secondMenu){
[Your second menu]
}

参见This ,它可以帮助您摆脱循环,第一个和第二个答案是解决您的问题的绝佳选择:)

关于java - 用户选择时使用 switch case 和 while 循环进行无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42347745/

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