Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何退出java方法_Java_Class_Methods_Logic - Fatal编程技术网

如何退出java方法

如何退出java方法,java,class,methods,logic,Java,Class,Methods,Logic,很抱歉标题不好,我不知道更好的解释 所以,我目前正在写一个杂货清单程序,我被我的addItemsToList方法卡住了。基本上,我希望它必须有“退出”点。其中一个将带您回到应用程序的主菜单,另一个将带您回到addItemsToList方法的上下文菜单。 不幸的是,我完全卡住了 以下是我的方法代码: private static void addItemsToList(){ System.out.println("To which category do you want to

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

所以,我目前正在写一个杂货清单程序,我被我的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():

现在发生的事情是,如果
choice2==5
我可以很好地退出addItemsToList,但是如果我等于“exit”,我似乎找不到退出的逻辑解决方案。我让它工作迅速和肮脏的应用程序首先要求数量,然后退出,但这不是我想要的


由于我(显然)是一个初学者,任何关于我的风格或其他方面的评论都将不胜感激

为此,您可以使用一个开关盒,一个知道何时执行优雅退出的标志,以及一个while循环来帮助您留在菜单中,直到用户提供正确的输入

例如:

公共类控制器{
私人杂货商杂货商;
private boolean正在运行;//我们的标志将帮助我们退出菜单
公共控制员(杂货控制员杂货控制员){
this.groceriesController=groceriesController;
}
公开募捐{
isRunning=true;
同时(正在运行){
清除屏幕();
displayLogo();
显示主菜单();
handleMainMenu();//将完成所有作业的方法;
}
}
私有void handleMainMenu(){
int userChoice=scanner.nextInt();
开关(用户选择){
案例1:
杂货控制器。handleGroceriesMenu();
打破
案例2:
returnItems();
打破
案例0:
isRunning=false;//从菜单中退出
打破
违约:
System.out.println(“无效输入”);
打破
}
}
私有void displayMainMenu(){
System.out.println(“\n杂货店:\n”
+“[1]买东西\n”
+“[2]退货\n”
+“[0]离开商店”);
}
}
公共类杂货控制器{
private boolean正在运行;//策略与以前相同
公共无效手册{
isRunning=true;
同时(正在运行){
清除屏幕();
displayLogo();
显示菜单();
handleMenu();
}
}
私有无效句柄菜单(){
int userChoice=scanner.nextInt();
开关(用户选择){
案例1:
附加部分(项目);
System.out.println(“已添加项:“+item+”);
打破
案例2:
从购物车(项目)中移除项目;
System.out.println(“项:“+item+”已被删除”);
打破
案例0:
isRunning=false;//从菜单中退出
打破
违约:
System.out.println(“无效输入”);
打破
}
}
私有void显示菜单(){
System.out.println(“\n选择:\n”
+“[1]购买物品\n”
+“[2]放回\n”
+“[0]离开”);
}
}
通过这种方式,您可以拥有多个相互调用的菜单,最后,程序自行完成,而不会出现异常返回;或系统。退出(0)


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

TL;DR
return
返回
将退出当前方法。虽然在您的情况下,我会将
if(choice2==5)
更改为
if(choice2!=5)
并使用它进入下一个逻辑流,否则只需让代码运行到方法的末尾,如果您添加
if(I.equalsIgnoreCase(“exit”){interact();},它将自动返回给调用者
在第二个出口点。
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");
    }