2016-02-12 63 views
1

我是初學者,當談到Java的。我有一個簡單的Java編程任務。這裏是該計劃的基礎:如何添加和多條語句在while循環中的Java

「最佳購買是接受電子設備和配件的在線訂單,他們開發了一個程序,允許用戶從一個項目菜單中選擇購買的設備。一個程序允許用戶(客戶)選擇要購買的物品,計算訂購物品的價格,並向客戶顯示該信息的收據(假設銷售稅率爲6.5%)「

您的程序應提示用戶(客戶)輸入以下信息:

  • 客戶名稱
  • 電子設備及配件購買

然後程序輸出以下信息:

  • 客戶姓名
  • 項目的總數排序
  • 價格的菜單項目前的營業稅
  • 營業稅
  • 總額由於

這裏是我的問題是如何,如果根據什麼數字用戶選擇我裏面添加我的嵌套的值是多少?

例句:用戶選擇1,2,3,10(要退出環路)

這裏是我的代碼:

import java.util.Scanner; 
public class OnlinePurchase { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     // TODO code application logic here 
     //Declare Variables 
     Scanner input = new Scanner(System.in); 
     String sCustomerName =""; //Stores the Customer's Name 
     int nSelection = 0;   //Stores the value entered by the user 
     int nSum = 0;    //Stores sum of values entered 
     int nCount = 0;    //Number of values entered 
     int nPrice = 0; 

     //Declare Constants 
     final int SENTINEL = 10; //Used to end loop 

     //Promt User to enter the their name. 
     System.out.print("Please enter the your name: "); 
     //Read Customer Name input from user 
     sCustomerName = input.nextLine();  

     //Print to Blank Line for spacing 
     System.out.println("\n"); 

     //Print Purchase Menu 
     System.out.println("BEST PURCHASE PRODUCTS:"); 
     System.out.println("1. Smartphone..........$249"); 
     System.out.println("2. Smartphone Case.... $39"); 
     System.out.println("3. PC Laptop...........$1149"); 
     System.out.println("4. Tablet..............$349"); 
     System.out.println("5. Tablet Case.........$49"); 
     System.out.println("6. eReader.............$119"); 
     System.out.println("7. PC Desktop..........$889"); 
     System.out.println("8. LED Monitor.........$299"); 
     System.out.println("9. Laser Printer.......$399"); 
     System.out.println("10.Complete my order"); 

     //Print to Blank Line for spacing 
     System.out.println("\n"); 

     while (nSelection != SENTINEL) { 

      //Calculate sum 
      nSum = nPrice + nSum; 

      //Increment counter 
      nCount++; //or nCount = nCount + 1; 

      //Promt User to enter the an item from the menu. 
      System.out.print("Please enter item from the menu above: "); 
      //Read input from user for selected purchase 
      nSelection = input.nextInt(); 

      if (nSelection == 1) { 

       nPrice = 249; 

       //Promt User to enter the an item from the menu. 
       System.out.print("Please enter another item from the menu above: "); 
       //Read input from user for selected purchase 
       nSelection = input.nextInt(); 
      } 
      else if (nSelection == 2) { 

       nPrice = 39; 

       //Promt User to enter the an item from the menu. 
       System.out.print("Please enter another item from the menu above: "); 
       //Read input from user for selected purchase 
       nSelection = input.nextInt(); 
      } 
      else if (nSelection == 3) { 

       nPrice = 1149; 

       //Promt User to enter the an item from the menu. 
       System.out.print("Please enter another item from the menu above: "); 
       //Read input from user for selected purchase 
       nSelection = input.nextInt(); 
      } 
      else if (nSelection == 4) { 

        nPrice = 349; 

        //Promt User to enter the an item from the menu. 
        System.out.print("Please enter another item from the menu above: "); 
        //Read input from user for selected purchase 
        nSelection = input.nextInt(); 
      } 
      else if (nSelection == 5) { 

       nPrice = 49; 

       //Promt User to enter the an item from the menu. 
       System.out.print("Please enter another item from the menu above: "); 
       //Read input from user for selected purchase 
       nSelection = input.nextInt(); 
      } 
      else if (nSelection == 6) { 

       nPrice = 119; 

       //Promt User to enter the an item from the menu. 
       System.out.print("Please enter another item from the menu above: "); 
       //Read input from user for selected purchase 
       nSelection = input.nextInt(); 
      } 
      else if (nSelection == 7) { 

       nPrice = 899; 

       //Promt User to enter the an item from the menu. 
       System.out.print("Please enter another item from the menu above: "); 
       //Read input from user for selected purchase 
       nSelection = input.nextInt(); 
      } 
      else if (nSelection == 8) { 

       nPrice = 299; 

       //Promt User to enter the an item from the menu. 
       System.out.print("Please enter another item from the menu above: "); 
       //Read input from user for selected purchase 
       nSelection = input.nextInt(); 
      } 
      else if (nSelection == 9) { 

       nPrice = 399; 

       //Promt User to enter the an item from the menu. 
       System.out.print("Please enter another item from the menu above: "); 
       //Read input from user for selected purchase 
       nSelection = input.nextInt(); 
      } 
     }//end while user did not enter 10 

     //Print blank line 
     System.out.println(); 

     //Print Thank You message 
     System.out.println("Thank you for ordering with Best Purchase,"+sCustomerName); 
     //Print number of integers entered 
     System.out.println("Total Items Ordered: " + nCount); 
     //Print number of integers entered 
     System.out.println("Total: $" + nSum); 
    }//end main class 
}//end public class 
+1

Stack Overflow是不是一門功課完成的服務。 – redFIVE

+1

首先,如果你想讓用戶選擇多個項目,你必須在循環中運行你的邏輯。其次,不是'nPrice = 119;'你可以做'nPrice + = 119;' - 例如... – alfasin

+0

@redFIVE你有沒有完全閱讀他的問題?他沒有要求我們去做他的HW。你應該收回你的失敗。 – alfasin

回答

2

說實話,你的代碼是在複雜,有很多的重複。 您應該利用重複使用的方法,或者使用switch語句來處理選擇合適的價格。此外,你不應該把所有的程序邏輯放在主要方法中 - 利用更小,更簡潔的方法,並儘量減少你放在main()中的內容。

while循環應該簡單地接受輸入的下一行,叫processItem()方法和終止,如果輸入的號碼是你的哨兵值(10)。

試試你的代碼的本次修訂:

import java.util.Scanner; 

public class OnlinePurchase { 

    String customerName; 
    int nSum = 0;    //Stores sum of values entered 
    int nCount = 0;    //Number of values entered 

    public void printPurchaseMenu(){ 
     //Print Purchase Menu 
     System.out.println("\nBEST PURCHASE PRODUCTS:"); 
     System.out.println("1. Smartphone..........$249"); 
     System.out.println("2. Smartphone Case.... $39"); 
     System.out.println("3. PC Laptop...........$1149"); 
     System.out.println("4. Tablet..............$349"); 
     System.out.println("5. Tablet Case.........$49"); 
     System.out.println("6. eReader.............$119"); 
     System.out.println("7. PC Desktop..........$889"); 
     System.out.println("8. LED Monitor.........$299"); 
     System.out.println("9. Laser Printer.......$399"); 
     System.out.println("10.Complete my order"); 
    } 

    public void setCustomerName(String name){ 
     customerName = name; 
    } 

    public void processInput(int inputValue){ 

     switch(inputValue){ 
      case 1: addItem(249); break; 
      case 2: addItem(39); break; 
      case 3: addItem(1149); break; 
      case 4: addItem(349); break; 
      case 5: addItem(49); break; 
      case 6: addItem(119); break; 
      case 7: addItem(889); break; 
      case 8: addItem(299); break; 
      case 9: addItem(399); break; 
      case 10: printResults(); break; 
      default: System.out.println("You entered an invalid value."); 
     } 
    } 

    public void addItem(int price){ 
     nSum += price; 
     nCount++; 
    } 

    public void printResults(){ 
     //Print Thank You message 
     System.out.println("\nThank you for ordering with Best Purchase," + customerName); 
     //Print number of integers entered 
     System.out.println("Total Items Ordered: " + nCount); 
     //Print number of integers entered 
     System.out.println("Total: $" + nSum); 
    } 


    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 

     OnlinePurchase onlinePurchase = new OnlinePurchase(); 

     //Declare Variables 
     Scanner input = new Scanner(System.in); 
     int nSelection = 0; //Stores the value entered by the user 

     //Declare Constants 
     final int SENTINEL = 10; //Used to end loop 

     //Prompt User to enter the their name. 
     System.out.print("Please enter the your name: "); 

     //Read Customer Name input from user 
     onlinePurchase.setCustomerName(input.nextLine()); 

     //Print Purchase Menu 
     onlinePurchase.printPurchaseMenu(); 

     while (nSelection != SENTINEL){ 
      System.out.print("Please enter item from the menu above: "); 
      nSelection = input.nextInt(); 

      onlinePurchase.processInput(nSelection); 
      System.out.println("\n"); 
     }//end while user did not enter 10 
    }//end main method 
}//end public class