2012-01-14 44 views
-1

我需要幫助的是這個,我通過創建下面的簡單銷售計算器來學習Jave。我有這樣的代碼中的部分如下:Java:存儲選定的值供以後使用

System.out.println ("Please make a selection:"); 
System.out.println ("1. Octopus Tentacles, 2. Rat Droppings"); 
System.out.println ("3. Sharks teeth, 4. Mountain Oysters"); 
System.out.println (" "); 
// Since we don't have a primary database to pull from we use a dummy DB 
while (foodCount <= 1) 
{ 
System.out.print("Enter selection (1 , 2, 3, 4): "); 
result = input.nextInt(); 

if (result == 1) 
    System.out.println("You selected Octopus tentacles, price $4.99"); 
else if (result == 2) 
    System.out.println("You selected Rat droppings, price $3.49"); 
else if (result == 3) 
    System.out.println("You selected Sharks teeth, price $6.73"); 
else if (result == 4) 
    System.out.println("You selected Mountain Oysters, price $9.95"); 
break; 
} 

System.out.println (" "); 

它允許選擇向我提出,我想要做的是保存在下面的輸入價格小有一點部分使用該值。有人能幫助我嗎?我是Java的新手,通過自己的閱讀方式學習自己的閱讀書籍,但沒有找到任何能夠幫助我的例子。任何援助與此將不勝感激。


import java.io.*; 
import java.text.*; 
import java.util.*; 
import java.util.Scanner; 
import java.math.BigDecimal; 
import java.text.NumberFormat; 

class priceCalc { 

public static void main (String[] args) throws IOException { 
    BufferedReader in = new BufferedReader (new InputStreamReader (System.in)); 
    Scanner input = new Scanner(System.in); 
    int Quantity; 
    int result = 0; 
    int foodCount = 1; 
    double Price, Subtotal, SalesTax, TotalSale; 
    NumberFormat curForm = NumberFormat.getCurrencyInstance(); 
// Here is a simple display of available products 
    System.out.println (" "); 
    System.out.println ("Welcome to the IP2 part 2!"); 
    System.out.println (" "); 

    System.out.println ("Please make a selection:"); 
    System.out.println ("1. Octopus Tentacles, 2. Rat Droppings"); 
    System.out.println ("3. Sharks teeth, 4. Mountain Oysters"); 
    System.out.println (" "); 
// Since we don't have a primary database to pull from we use a dummy DB 
    while (foodCount <= 1) 
    { 
    System.out.print("Enter selection (1 , 2, 3, 4): "); 
    result = input.nextInt(); 

    if (result == 1) 
    System.out.println("You selected Octopus tentacles, price $4.99"); 
    else if (result == 2) 
    System.out.println("You selected Rat droppings, price $3.49"); 
    else if (result == 3) 
    System.out.println("You selected Sharks teeth, price $6.73"); 
    else if (result == 4) 
    System.out.println("You selected Mountain Oysters, price $9.95"); 
    break; 
    } 

    System.out.println (" "); 
    // Create Scanner for input 
    Scanner key = new Scanner(System.in); 

    //Quantity 
    System.out.print ("Please enter quantity: "); 
    Quantity = key.nextInt(); 
    System.out.println (" "); 

    //Price  
    System.out.print ("Please input price (price amount w/no $): "); 
    Price = key.nextDouble(); 
    System.out.println (" "); 

    //Subtotal 
    Subtotal = Price * Quantity; 
    System.out.println ("Your subtotal is: " +curForm.format(Subtotal)); 

    //Sales Tax 
    SalesTax = Subtotal * .065; 
    System.out.println ("Your sales tax is: " +curForm.format(SalesTax)); 

    //Total Sale 
    TotalSale = Subtotal+SalesTax; 
    System.out.println ("Your total sale price is: " +curForm.format(TotalSale)); 
    System.out.println (" "); 
} 
} 
+0

我仍然沒有得到問題的地方 – 2012-01-14 17:17:52

+0

Theres沒有代碼的問題,它的工作方式,它工作正常。問題是我想要自動化價格,所以當我選擇1,2,3,4以上時,價格將被存儲並在添加數量的數量後被調用爲以下價格的輸入。 – Powercat 2012-01-14 17:21:20

回答

0

通常情況下,我期望一個初學者錯誤是result不合適的作用域 - 在這裏你會的東西儲存到它,但不能從父範圍內訪問它。但是,result在此適當限定範圍。

你的例子看起來不完整,因爲你試圖循環(在foodCount <= 1) - 但你總是打破,所以你永遠不會實際循環。因此,您應該只有一個選擇--的值在您的後續代碼中可用於您的計算 - 正好在Price,Subtotal,SalesTaxTotalSale旁邊。 (遵循Java約定,這些變量名應該全部以小寫字母開頭)。但是,我很困惑爲什麼在用戶已經選擇了一個具有相關價格的物品之後,您提示輸入價格 - 除非這是您正在努力糾正。

+1對於John的建議/回答。在此之後,您可以刪除您的價格提示。但是,您需要首先聲明price以及其他變量才能使其工作。

+0

是的,我想改正這一點。我又是新來的Java和我正在學習,因爲我將與SQL合作我想慢慢地獲得更多的Java技能,所以我最終可以學習如何連接這兩個。 – Powercat 2012-01-14 17:28:46

1

我已經做了相關修改,並給出了它們的理由。


class priceCalc { 
    public static void main (String[] args) throws IOException { 
     /* 
     You don't need a BufferedReader and also a Scanner, any one will do. 
     BufferedReader in = new BufferedReader (new InputStreamReader (System.in)); 
     */ 

     Scanner input = new Scanner(System.in); 

     /* 
     Changed variable names to camelCase 
     http://en.wikipedia.org/wiki/Naming_convention_%28programming%29#Java 
     */ 
     int quantity; 
     int result = 0; 
     double price[] = new double[4]; 
     double subTotal, salesTax, totalSale; 
     NumberFormat curForm = NumberFormat.getCurrencyInstance(); 
     // Here is a simple display of available products 
     System.out.println (" "); 
     System.out.println ("Welcome to the IP2 part 2!"); 
     System.out.println (" "); 
     System.out.println("Enter prices for each product"); 
     System.out.println("Octopus tentacles: "); 
     price[0] = input.nextDouble(); 
     System.out.println("Rat Droppings: "); 
     price[1] = input.nextDouble(); 
     System.out.println("Sharks teeth: "); 
     price[2] = input.nextDouble(); 
     System.out.println("Mountain Oysters: "); 
     price[3] = input.nextDouble(); 

     System.out.println ("Please make a selection:"); 
     System.out.println ("1. Octopus Tentacles, 2. Rat Droppings"); 
     System.out.println ("3. Sharks teeth, 4. Mountain Oysters"); 
     System.out.println (" "); 
     // Changed while into an infinite loop that will break on any input other than 1, 2, 3, and 4. 
     while (true) 
     { 
      System.out.print("Enter selection (1 , 2, 3, 4): "); 
      result = input.nextInt(); 
      if (result == 1) 
      { 
       System.out.println("You selected Octopus tentacles, "+price[0]); 
       subTotal = price[0]*getQuantity(); 
      } 
      else if (result == 2) 
      { 
       System.out.println("You selected Rat droppings, "+price[1]); 
       subTotal = price[1]*getQuantity(); 
      } 
      else if (result == 3) 
      { 
       System.out.println("You selected Sharks teeth, "+price[2]); 
       subTotal = price[2]*getQuantity(); 
      } 
      else if (result == 4) 
      { 
       System.out.println("You selected Mountain Oysters, "+price[3]); 
       subTotal = price[3]*getQuantity(); 
      } 
      else 
      { 
       break; 
      } 
      System.out.println ("Your subTotal is: " +curForm.format(subTotal)); 
      salesTax = subTotal * .065; 
      System.out.println ("Your sales tax is: " +curForm.format(salesTax)); 
      totalSale = subTotal+salesTax; 
      System.out.println ("Your total sale price is: " +curForm.format(totalSale)); 
      System.out.println (" "); 
     } 

     System.out.println (" "); 
     /* 
     You don't need a second Scanner within the same scope 
     Scanner key = new Scanner(System.in); 
     */ 
    } 
    public static int getQuantity() 
    { 
     Scanner input = new Scanner(System.in); 
     System.out.println("Enter quantity: "); 
     int q = input.nextInt(); 
     return q; 
    } 
} 
+0

@Powercat - 如約翰在他的回答中所提供的,你最好總是在所有的條件中使用大括號,甚至是一行。 – ziesemer 2012-01-14 17:28:04

+0

我在這裏理解這個編碼,但是,我該如何聲明「價格」才能起作用? – Powercat 2012-01-14 17:39:39

+0

好吧我得到它聲明和它的設置沒有錯誤,直到我運行它。我已經刪除了輸入(註釋),但不知道如何設置參數,以便聲明的值進入所需的空間:System.out.print(「請輸入價格(price amount w/no $):」); // Price = key.nextDouble(); System.out.println(「」); – Powercat 2012-01-14 17:48:16

相關問題