2014-09-25 53 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

相关问题