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

时间:2017-09-14 06:12:24

标签: 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 = 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();

 }
}

2 个答案:

答案 0 :(得分:2)

更改此

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

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

也在您的其他屏蔽块中catch(Exception e){ System.out.println("Please enter a valid integer"); continue; } 代替continue

答案 1 :(得分:1)

你永远不会只用一个帖子离开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;
    }

“请输入与上下文相关的选项”消息应该真正打印在else语句中。因为在您的if中,您已经检查parse是否介于1和7之间,因此在切换中,parse不能是其他任何内容,因此永远不会到达default分支。打印消息后,您continue;以返回循环的开头。