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

Is it possible to exit from a main method without terminating the program?

我知道 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();

 }
}

改变这个

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

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

也在你的 else 块中有 continue 而不是 return

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

continue关键字将停止执行封闭循环的当前迭代并立即开始新的迭代。这是您应该用来代替 return.

的内容
    try{
     parse = Integer.parseInt(choice);
      }
    catch(Exception e){
        System.out.println("Please enter a valid integer"); 
        return;  // <--- change this to "continue;"
    }

另外,这个:

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

"Please enter choice relevant to context" 消息应该真正打印在 else 语句中。因为在你的 if 中,你已经检查过 parse 是否在 1 和 7 之间,所以在 switch 中,parse 不能是其他任何东西,所以永远不会到达 default 分支。打印消息后,您 continue; 以便返回到循环的开始。