加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

如何在没有System.exit的情况下退出Java程序?(来自用户输入?)

发布时间:2020-12-15 05:09:52 所属栏目:Java 来源:网络整理
导读:这是我的代码.我不知道如何使用返回值退出程序.有任何想法吗?这是我任务的最后一步. 重要区域标有/////// 我听说返回工作但是当我将main中的void更改为int时,程序说main必须是void. import java.util.Scanner;public class CommissionCalculator { public s
这是我的代码.我不知道如何使用返回值退出程序.有任何想法吗?这是我任务的最后一步.
重要区域标有///////
我听说返回工作但是当我将main中的void更改为int时,程序说main必须是void.

import java.util.Scanner;


public class CommissionCalculator {

    public static void main(String args[]) {
        // Initialize a Scanner to read input from the command line
        Scanner ItemSelect = new Scanner(System.in);
        double comp = 200.00;
        double item1 = 239.99;
        double item2 = 129.75;
        double item3 = 99.95;
        double item4 = 350.89;
        double comm = 0.09;
        int choice;


        /* Note that we'll be doing this at least once and most likely multiple times...
         * Prompt the user with a menu of the four items and their values (this information is included in the problem statement)
         */
        System.out.println("ItemtValue");
        System.out.println("1t$239.99");
        System.out.println("2t$129.75");
        System.out.println("3t$99.95");
        System.out.println("4t$350.89"); 
        /* Display the user's current compensation */
        System.out.printf("Current compensation: $%.2f",comp);


        /* 
         * Prompt and take input from the user (you may assume that they will only enter int values)
         * They'll enter an item number (1 - 4) to record its sale or 0 to exit
         * 
         * NOTE: THE U0SER DOES NOT ENTER PRICES DIRECTLY... THEY ENTER ITEM NUMBERS TO INDICATE WHAT WAS SOLD
         * NOTE: THE USER MAY ENTER THE SAME ITEM NUMBRER MULTIPLE TIMES
         * 
         * If the user provides invalid input (a value other than 0 - 4) display "ERROR: Invalid input!" and prompt them again
         */
        do
        {

            System.out.print("nPlease select an item from the " +
                               "list above (or enter 0 to exit): ");
            choice = ItemSelect.nextInt();

            {   
                if (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 0)
                {
                    do
                      {
                        System.out.print("ERROR: Invalid Input!n");
                        System.out.println("ItemtValue");
                        System.out.println("1t$239.99");
                        System.out.println("2t$129.75");
                        System.out.println("3t$99.95");
                        System.out.println("4t$350.89");
                        System.out.printf("Current compensation: $%.2f",comp);
                        System.out.print("nPlease select an item from the " +
                                   "list above (or enter 0 to exit): ");
                        choice = ItemSelect.nextInt();
                        if (choice == 1)
                        {
                            comp += (comm * item1);
                            System.out.printf("Current compensation: $%.2f",comp);
                        }
                        if (choice == 2)
                        {
                            comp += (comm * item2);
                            System.out.printf("Current compensation: $%.2f",comp);
                        }
                        if (choice == 3)
                        {
                            comp += (comm * item3);
                            System.out.printf("Current compensation: $%.2f",comp);
                        }
                        if (choice == 4)
                        {
                            comp += (comm * item4);
                            System.out.printf("Current compensation: $%.2f",comp);
                        }
                        if (choice == 0)
                        {
                            System.out.printf("Total Earnings: $%.2f",comp);
                            System.exit(0); ///////
                        }

                      }while (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 0); 

                }
                else
                {
                    if (choice == 1)
                    {
                        comp += (comm * item1);
                        System.out.printf("Current compensation: $%.2f",comp);
                    }
                    if (choice == 2)
                    {
                        comp += (comm * item2);
                        System.out.printf("Current compensation: $%.2f",comp);
                    }
                    if (choice == 3)
                    {
                        comp += (comm * item3);
                        System.out.printf("Current compensation: $%.2f",comp);
                    }
                    if (choice == 4)
                    {
                        comp += (comm * item4);
                        System.out.printf("Current compensation: $%.2f",comp);
                    }
                    if (choice == 0)
                    {
                        System.out.printf("Total Earnings: $%.2f",comp);
                        System.exit(0);
                    }
                }

            }
        }while (choice != 0);

        /* After the user enters 0,display the salesperson's earnings in the format "Total earnings: $NNN.NN" and exit
         * For example,if the salesperson sold two item 3s this week the final output would be "Total earnings: $217.99"
         */
        if (choice == 0)
        {
            System.out.printf("Total Earnings: $%.2f",comp);
            System.exit(0);   ///////
        }
        ItemSelect.close();
        System.exit(0);    ///////
    }
}

解决方法

只要您不需要返回自定义退出代码(除了0,由System.exit(0)返回)并且不启动新线程,您可以通过执行来终止程序

return;

在你的main()方法中.请注意,没有返回值,因此您无需将main()方法从void更改为int.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读