2014-09-25 54 views
0
import java.util.Scanner; 

public class CoffeeShop { 

public static void main(String[] args) { 
//Welcome message 
String username; 

System.out.println("Please enter your name"); 
Scanner keyboard = new Scanner(System.in); 
username = keyboard.next(); 
System.out.println("Welcome to the Java Byte Code Coffee Shop," + username + "!"); 

//Menu 
System.out.println("Here is our menu."); 
System.out.println("1. Coffee $1.50"); 
System.out.println("2. Latte $3.50"); 
System.out.println("3. Cappuccino $3.25"); 
System.out.println("4. Expresso $2.00"); 

//Item selection 
int item_Number; 
System.out.println("Please enter an item number."); 
Scanner item = new Scanner(System.in); 
item_Number = item.nextInt(); 


if(item_Number == 1) { 
item = 1.50; 
} 
if(item_Number == 2) { 
item = 3.50; 
} 
if(item_Number == 3) { 
item = 3.25; 
} 
if(item_Number == 4) { 
item = 2.00; 
} 

//Item Quantity 
int quantity; 
System.out.println("Please enter the quantity."); 
Scanner amount = new Scanner(System.in); 
quantity = amount.nextInt(); 
double total = quantity * item; 
System.out.println("Total before discount and tax is " + total); 
//Discount and tax 
double nuTotal; 
if(total >= 10) { 
nuTotal = total - (total * .1); 
} else { 
nuTotal = total; 
} 
System.out.println("Your total with discount is " + nuTotal); 
double totalTax = nuTotal * .07; 
System.out.println("Your total with tax is " + totalTax); 
System.out.println("Thank you " + username + "! Please stop by again!"); 

} 
} 

您的任務是編寫一個名爲CoffeeShop的程序(在CoffeeShop.java文件中),該程序模擬虛擬咖啡店,允許用戶選擇要購買的物品以及數量該項目。在從用戶處獲得物品選擇和所需數量後,程序應計算成本,稅金,折扣和最終成本,然後將這些數據顯示給用戶。我每次嘗試編譯程序時都會收到多個錯誤。不知道我錯在哪裏

+2

你得到的多個錯誤是什麼? – APerson 2014-09-25 02:00:36

+0

檢查你的「item」變量..一旦它是一個掃描儀,然後你用它來試圖存儲一個double。 – 2014-09-25 02:01:09

回答

1

問題從這裏開始:

Scanner item = new Scanner(System.in); 
item_Number = item.nextInt(); 


if(item_Number == 1) { 
item = 1.50; 
} 
if(item_Number == 2) { 
item = 3.50; 
} 
if(item_Number == 3) { 
item = 3.25; 
} 
if(item_Number == 4) { 
item = 2.00; 
} 

itemScanner對象,但然後嘗試給它一個double值。

然後double total = quantity * item;。您乘以Scanner對象。

0

您的錯誤代碼如下。

首先聲明:

Scanner item = new Scanner(System.in); 

然後嘗試設置掃描儀變量是什麼,我推測是價格。

if(item_Number == 1) { 
item = 1.50; 
} 
if(item_Number == 2) { 
item = 3.50; 
} 
if(item_Number == 3) { 
item = 3.25; 
} 
if(item_Number == 4) { 
item = 2.00; 
} 

爲了解決這個問題,你應該做的是設置這些號碼一個新的變量是什麼,如:

double total = quantity * price; 

double price = 0; 

price = ...; //not: item = ...; 

然後你可以簡單地計算出您的總

希望這有助於!剛剛嘗試設置變量:)

我相信你的代碼應該是類似的時候要非常小心:

double price = 0; 
if(item_Number == 1) { 
    price = 1.50; 
} 
if(item_Number == 2) { 
    price = 3.50; 
} 
if(item_Number == 3) { 
    price = 3.25; 
} 
if(item_Number == 4) { 
    price = 2.00; 
} 

double total = quantity * price; 
+0

我確實是你上面發佈的,現在我得到這個錯誤: CoffeeShop.java:47:錯誤:可能的價格可能沒有初始化 double total = quantity * price; ^ – 2014-09-25 02:12:42

+0

@RobertSemulka你用'double price = 0;'聲明瞭它嗎?好點,它可能永遠不會被設置 – StephenButtolph 2014-09-25 02:14:04

+0

是的,我做到了。這就是爲什麼我不知道爲什麼我得到那個錯誤 – 2014-09-25 02:16:33

0

這會幫助你一點!

import java.text.DecimalFormat; 
import java.util.Scanner; 

public class JavaApplication2 { 

public static void main(String[] args) { 

    //Welcome message 
    String username; 
    double item = 0.0; 
    Scanner keyboard = new Scanner(System.in); 
    DecimalFormat df = new DecimalFormat("#.00"); 

    System.out.println("Please enter your name");   
    username = keyboard.next(); 
    System.out.println("Welcome to the Java Byte Code Coffee Shop," + username + "!"); 

    //Menu 
    System.out.println("Here is our menu."); 
    System.out.println("1. Coffee $1.50"); 
    System.out.println("2. Latte $3.50"); 
    System.out.println("3. Cappuccino $3.25"); 
    System.out.println("4. Expresso $2.00"); 

    //Item selection 
    int item_Number = 0; 
    System.out.println("Please enter an item number."); 
    item_Number = keyboard.nextInt(); 

    if (item_Number == 1) { 
     item = 1.50; 
    } 
    if (item_Number == 2) { 
     item = 3.50; 
    } 
    if (item_Number == 3) { 
     item = 3.25; 
    } 
    if (item_Number == 4) { 
     item = 2.00; 
    } 

    //Item Quantity 
    int quantity = 0; 
    System.out.println("Please enter the quantity."); 
    quantity = keyboard.nextInt(); 
    double total = quantity * item;   
    System.out.println("Total before discount and tax is $ " + df.format(total)); 

    //Discount and tax 
    double nuTotal; 
    if (total >= 10) { 
     nuTotal = total - (total * 0.10); 
    } else { 
     nuTotal = total; 
    } 
    System.out.println("Your total with discount is $" + df.format(nuTotal)); 
    double totalTax = nuTotal + (nuTotal * 0.07); 
    System.out.println("Your total with tax is: $" + df.format(totalTax)); 
    System.out.println("Thank you " + username + "! Please stop by again!"); 

} 

}

我建議你遵循名稱模式的Java編程語言和它的變量...你可以看到在文檔中的位置:http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html

我也建議你初始化你變量以便在驗證時避免空值。 希望這有助於!它已經在工作......但我不確定你的期望是什麼,祝你好運!

+0

折扣和稅前總額是24.0 您的折扣總額是21.6 您的總稅額是:1.5120000000000002 謝謝搶!請再次停下來! 我如何獲得數值讀取$ 24.00?總稅也是錯的......我該如何解決這個問題? – 2014-09-25 02:30:38

+0

@RobertSemulka好吧,先生爲了格式化你使用DecimalFormat類的「雙」輸出。關於價值有一個總和失蹤,因爲totalTax必須如下:double totalTax = nuTotal +(nuTotal * 0.07);請檢查上面的代碼更改! – 2014-09-25 02:54:06

相關問題