Java 每次试图编译程序时,我都会遇到多个错误。不知道我哪里出错了

Java 每次试图编译程序时,我都会遇到多个错误。不知道我哪里出错了,java,Java,您的任务是编写一个名为CoffeeShop(在CoffeeShop.java文件中)的程序,该程序模拟一个虚拟咖啡馆,允许用户选择要购买的商品以及该商品的数量。从用户处获得项目选择和所需数量后,您的程序应计算成本、税费、折扣和最终成本,然后将其显示给用户。问题从这里开始: import java.util.Scanner; public class CoffeeShop { public static void main(String[] args) { //Welcome message

您的任务是编写一个名为CoffeeShop(在CoffeeShop.java文件中)的程序,该程序模拟一个虚拟咖啡馆,允许用户选择要购买的商品以及该商品的数量。从用户处获得项目选择和所需数量后,您的程序应计算成本、税费、折扣和最终成本,然后将其显示给用户。

问题从这里开始:

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!");

}
}
是一个
扫描器
对象,但您尝试为其分配一个
双值


然后
双倍合计=数量*项目。你乘以一个扫描器对象。

你的错误在下面的代码中

首先你要声明:

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; 
}
然后你试着将scanner变量设置为我认为的价格

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 price = 0;

price = ...; //not: item = ...;
希望这有帮助!在设置变量时要非常小心:)

我相信您的代码应该类似于:

double total = quantity * price;

这应该对你有点帮助

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;
}

我建议您遵循Java编程语言及其变量的名称模式。。。您可以在此处查看文档:

我还建议您初始化变量,以避免验证时出现空值。
希望这有帮助!它已经开始工作了。。。但我不确定你的期望是什么,祝你好运

您遇到的多个错误是什么?请检查“item”变量。。一旦它是一个扫描器,你就可以用它来存储一个double^@罗伯特梅尔卡:您是否使用
double price=0?很好的一点是,这可能永远不会发生。是的,我做了。这就是为什么我不确定为什么我会收到那个错误。不管怎样,它现在可以编译了。。折扣和税收前的总额为21.0您的折扣总额为18.9您的税收总额为1.323谢谢Robert!请再来一次!我需要打印每个计算的美元金额(例如税额),精确到小数点后两位,并且我必须用美元符号显示每个美元金额。。你知道如何做到这一点吗?折扣和税收前的总额是24.0你的折扣总额是21.6你的税收总额是:1.5120000000002谢谢你,罗布!请再来一次!如何将数值设置为$24.00?还有,含税的总数是错误的……我该如何解决这个问题?@RobertSemulka好的,先生,为了格式化您的“双精度”输出,您使用DecimalFormat类。关于数值,缺少一个总和,因为totalTax必须如下所示:double totalTax=nuTotal+(nuTotal*0.07);请检查上面的代码更改!
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!");

}