Java 是否可以在不终止程序的情况下退出主方法?

Java 是否可以在不终止程序的情况下退出主方法?,java,return,main,exit,Java,Return,Main,Exit,我知道return语句,并且已经尝试过了。System.exit(0)也会执行相同的操作。但是在这里使用它会终止程序。有没有什么方法可以让我使用,如果用户输入除1-7之外的其他输入,程序不会终止,这样我就不必重新编译并重新运行程序?或者在Java中是不可能的 import java.util.Scanner; public class NewShoppingCart{ public static void main(String args[]) { boolean flag = tru

我知道return语句,并且已经尝试过了。System.exit(0)也会执行相同的操作。但是在这里使用它会终止程序。有没有什么方法可以让我使用,如果用户输入除1-7之外的其他输入,程序不会终止,这样我就不必重新编译并重新运行程序?或者在Java中是不可能的

import java.util.Scanner;
public class NewShoppingCart{
public static void main(String args[]) {

    boolean flag = true;
    long code;
    String choice;
    NewShop aShop = new NewShop();
    Scanner sc = new Scanner(System.in);
    Integer parse = 0;

    System.out.println("-----ITEM------");
    do {
        System.out.println("1. Display all items");
        System.out.println("2. Search items");
        System.out.println("3. Add items to list");
        System.out.println("4. Add items to cart");
        System.out.println("5. Display cart");
        System.out.println("6. Issue item");
        System.out.println("7. Exit");
        System.out.println("Choice:");
        choice = sc.nextLine();

        try{
         parse = Integer.parseInt(choice);
          }
        catch(Exception e){
            System.out.println("Please enter a valid integer"); 
            return;  
        }
        if (parse >=1 && parse <= 7 )
         {

        switch (parse) {

        case 1:
            aShop.display();
            break;

        case 2:
            aShop.searchItem();
            break;

        case 3:
            aShop.addItem();
            break;

        case 4:
            aShop.addItemtoCart();
            break;


        case 5:
            aShop.displayCart();
            break;

        case 6:
            aShop.issueItem();
            break;

        case 7:
            System.out.println("Thank you!\n");
            flag = false;
            break;

        default :
             System.out.println("Please enter choice relevant to context");
          }
        }
     else   return;
       }

     while (flag != false);
    sc.close();

 }
}
import java.util.Scanner;
公共类新闻购物车{
公共静态void main(字符串参数[]){
布尔标志=真;
长代码;
字符串选择;
新闻商店aShop=新建新闻商店();
扫描仪sc=新的扫描仪(System.in);
整数解析=0;
System.out.println(“----项----”);
做{
System.out.println(“1.显示所有项目”);
System.out.println(“2.搜索项”);
System.out.println(“3.将项目添加到列表中”);
System.out.println(“4.将项目添加到购物车”);
System.out.println(“5.显示车”);
系统输出打印项次(“6.发行项目”);
System.out.println(“7.Exit”);
System.out.println(“选项:”);
choice=sc.nextLine();
试一试{
parse=Integer.parseInt(选项);
}
捕获(例外e){
System.out.println(“请输入有效整数”);
回来
}
如果(parse>=1&&parse更改此值

catch(Exception e){
            System.out.println("Please enter a valid integer"); 
            return;  
        }


另外,在else块中,使用
continue
而不是
return

你永远不能只用一个线程就离开
main
。这可能是XY问题。如果用户输入了无效的内容,你真正想要的是回到循环的开始

    try{
     parse = Integer.parseInt(choice);
      }
    catch(Exception e){
        System.out.println("Please enter a valid integer"); 
        return;  // <--- change this to "continue;"
    }
continue
关键字将停止执行封闭循环的当前迭代,并立即开始新的迭代。这是您应该用来代替
return

    try{
     parse = Integer.parseInt(choice);
      }
    catch(Exception e){
        System.out.println("Please enter a valid integer"); 
        return;  // <--- change this to "continue;"
    }
试试看{
parse=Integer.parseInt(选项);
}
捕获(例外e){
System.out.println(“请输入有效整数”);

return;/=1&&parse=1&&parse-nop,这是应用程序的主要入口…一旦你返回该方法,应用程序就消失了!只需记录异常,不要从那里返回,那么你的while循环将继续并再次请求输入。当程序终止时,你不需要重新编译程序。除非程序删除自身。你可以nt解释这有什么帮助(我同意它是这样的)默认值:System.out.println(“\n”);}否则System.out.println(“请输入1到7之间的选项”);继续;}我想这样就可以了。因为有人建议我为了“良好实践”而使用默认情况。您可以删除默认大小写,因为在这种情况下,它将永远无法到达。if语句已检查1-7之外的值。@Shachindratripath其他情况下使用默认大小写会更好,因为这样更容易知道某些内容是否与您指定的所有大小写都不匹配。这有助于调试。@Shachindratripath如果您认为我的回答你的问题,请考虑点击支票。
if (parse >=1 && parse <= 7 )
    {

        switch (parse) {

            case 1:
                aShop.display();
                break;

            case 2:
                aShop.searchItem();
                break;

            case 3:
                aShop.addItem();
                break;

            case 4:
                aShop.addItemtoCart();
                break;


            case 5:
                aShop.displayCart();
                break;

            case 6:
                aShop.issueItem();
                break;

            case 7:
                System.out.println("Thank you!\n");
                flag = false;
                break;

            default :
                System.out.println("Please enter choice relevant to context");
        }
    }
    else return;
    if (parse >=1 && parse <= 7 )
    {

        switch (parse) {

            case 1:
                aShop.display();
                break;

            case 2:
                aShop.searchItem();
                break;

            case 3:
                aShop.addItem();
                break;

            case 4:
                aShop.addItemtoCart();
                break;


            case 5:
                aShop.displayCart();
                break;

            case 6:
                aShop.issueItem();
                break;

            case 7:
                System.out.println("Thank you!\n");
                flag = false;
                break;
        }
    }
    else {
        System.out.println("Please enter choice relevant to context");
        continue;
    }