gpt4 book ai didi

java - 如何退出java方法

转载 作者:行者123 更新时间:2023-12-01 18:31:31 26 4
gpt4 key购买 nike

抱歉,标题不好,我不知道更好的解释。

我目前正在编写一个杂货 list 程序,但我陷入了我的 addItemsToList 方法。基本上我希望它必须有“退出”点。其中之一将带您返回应用程序的主菜单,其中之一将带您返回该 addItemsToList 方法的上下文菜单。不幸的是我完全陷入困境。

这是我的方法代码:

 private static void addItemsToList(){
System.out.println("To which category do you want to add? ");
categoryInstructions();
int choice2 = scan.nextInt();

if(choice2 == 5){ // i am "getting out" here just fine
interact(); // this is the method that instructs everything to happen in the main method
}else {
System.out.println("What do you want to add? Type 'exit' for menu");
i = scan.next(); // this is where the second "exit" point is supposed to be if someone types "exit"

if (i.compareTo(exit) != 0) {
System.out.println("Please enter the quantity ");
int quant = scan.nextInt();

if (choice2 == 1) {
GrocerieList.foodstuffsList.add(i);
GrocerieList.foodstuffsAmount.add(quant);
} else if (choice2 == 2) {
GrocerieList.hygeneList.add(i);
GrocerieList.hygeneAmount.add(quant);
} else if (choice2 == 3) {
GrocerieList.drinkList.add(i);
GrocerieList.drinkAmount.add(quant);
} else if (choice2 == 4) {
GrocerieList.otherList.add(i);
GrocerieList.otherAmount.add(quant);
}
} else {
addItemsToList();
}
}
}

为了更好地理解这里是categoryInstructions():

public static void categoryInstructions(){
System.out.println("Press");
System.out.println(" ");
System.out.println("\t\t" + "(1)" + "\t--\t" + "for foodstuffs");
System.out.println("\t\t" + "(2)" + "\t--\t" + "for hygene");
System.out.println("\t\t" + "(3)" + "\t--\t" + "for drinks");
System.out.println("\t\t" + "(4)" + "\t--\t" + "for others");
System.out.println(" ");
System.out.println("\t\t" + "(5)" + "\t--\t" + "to get back main menu");
}

现在发生的事情是,如果choice2 == 5,我可以完美退出addItemsToList,但如果i等于“exit”,我似乎找不到退出的逻辑解决方案。我让它工作得又快又脏,应用程序首先询问数量,然后退出,但这并不是我想要的。

由于我(显然)是初学者,因此对我的风格或其他方面的任何其他评论也将不胜感激!!

最佳答案

您可以使用 switch case 来实现此目的,使用一个标志来了解何时执行优雅的退出,并使用 while 循环来帮助您留在菜单中,直到用户提供正确的输入。

例如:

public class Controller {

private GroceriesController groceriesController;
private boolean isRunning; // our flag that will help us to quit the menu

public Controller(GroceriesController groceriesController) {
this.groceriesController = groceriesController;
}

public void run() {
isRunning = true;
while (isRunning) {
clearScreen();
displayLogo();
displayMainMenu();
handleMainMenu(); // method that will do all the job;
}
}

private void handleMainMenu() {
int userChoice = scanner.nextInt();
switch (userChoice) {
case 1:
groceriesController.handleGroceriesMenu();
break;
case 2:
returnItems();
break;
case 0:
isRunning = false; // exit from the menu
break;
default:
System.out.println("Invalid input");
break;
}

}

private void displayMainMenu() {
System.out.println("\n Grocery store:\n"
+ "[1] Buy stuff\n"
+ "[2] Return stuff\n"
+ "[0] Leave store");
}
}

public class GroceriesController {

private boolean isRunning; // same strategy as before

public void handleGroceriesMenu() {
isRunning = true;
while (isRunning) {
clearScreen();
displayLogo();
displayMenu();
handleMenu();
}
}

private void handleMenu() {
int userChoice = scanner.nextInt();
switch (userChoice) {
case 1:
addItemToCart(item);
System.out.println("The item: " + item + "has been added.");
break;
case 2:
removeItemFromCart(item);
System.out.println("The item: " + item + "has been removed.");
break;
case 0:
isRunning = false; // exit from the menu
break;
default:
System.out.println("Invalid input");
break;
}

}

private void displayMenu() {
System.out.println("\n Select:\n"
+ "[1] Buy item\n"
+ "[2] Put back\n"
+ "[0] Leave");
}

}

通过这种方式,您可以拥有多个相互调用的菜单,最后,程序自行完成,无需疯狂返回;或 System.exit(0);

编辑:添加了一个类以获得更好的洞察力。

关于java - 如何退出java方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60158278/

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