4

これが私のコードです。戻り値を使用してプログラムを終了する方法がわかりません。何か案は?これが私の任務の最後のステップです。重要な領域は///////でマークされていますリターンワークが聞こえますが、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("Item\tValue");
        System.out.println("1\t$239.99");
        System.out.println("2\t$129.75");
        System.out.println("3\t$99.95");
        System.out.println("4\t$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("Item\tValue");
                        System.out.println("1\t$239.99");
                        System.out.println("2\t$129.75");
                        System.out.println("3\t$99.95");
                        System.out.println("4\t$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);    ///////
    }
}
4

2 に答える 2

14

カスタム終了コード(によって返される0以外System.exit(0))を返す必要がなく、新しいスレッドを開始しない限り、次のようにしてプログラムを終了できます。

return; 

あなたのmain()方法で。main()値は返されないため、メソッドをからvoidに変更する必要はありませんint

于 2012-11-06T08:00:32.987 に答える
0

コードが適切に流れる論理的な方法でプログラムを記述した場合、メソッドが最後に何かを返すことになっている場合を除いて、exit()またはreturnは必要ありません。その場合、returnステートメントは1つだけになります。メソッドの最後で、たとえば、とにかく非効率的な構造の場合、スタックではなく、0のケースを持つスイッチを使用します。ケースはすべての計算と出力を実行し、ケース0は、不要なexit()呼び出しを除いて、ループが終了した後に実行する処理を実行します。ループが終了すると、メソッドは終了します。

于 2021-09-26T19:55:15.570 に答える