2012-11-06 37 views
3

這是我的代碼。我不知道如何使用返回值退出程序。有任何想法嗎?這是我分配的最後一步。 重要區域標有/////// 我聽到返回工程,但是當我將void更改爲int時,程序說main必須爲void。如何退出Java程序而無System.exit?(來自用戶輸入?)

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); /////// 
    } 
} 
+2

將值返回給調用程序的唯一方法是使用System.exit。例如,如果你想從另一個類訪問這個方法,那麼你應該把內容移動到另一個能夠返回你想要的值的方法,然後你可以從你的主方法中得到這個,如果你需要或從另一個類。這樣,你可以使用返回 – MadProgrammer

+0

這就是它。你爲什麼不回答,我解決了。 – user1781382

+0

我沒有回答,因爲這個問題有點模糊(恕我直言),但如果你喜歡評論,給它一點投票;) – MadProgrammer

回答

13

只要你並不需要返回自定義退出代碼(0以外,通過System.exit(0)返回),並且不會開始新的線程,你可以通過做

return; 
終止程序

你的main()方法。請注意,沒有返回值,因此您無需將main()方法從void更改爲int

+0

工作得很好。 TYVM – user1781382

相關問題