java - 无法解释的无法编译源代码错误

标签 java

尝试运行以下程序但不断出现错误。我不确定我哪里出错了。任何帮助将不胜感激。

run:

Welcome to Mike and Diane's Pizza

Enter your first name: Mike

Pizza Size (inches ) Cost:

10 $10.99

12 $12.99

14 $14.99

16 $16.99

What size pizza would you like?

10, 12, 14, or 16 (enter the number only): 12

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code

Java 结果:1 构建成功(总时间:22 秒)

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Scanner;
// You have to add an import statement to use the DecimalFormat class
import java.text.DecimalFormat;

public class Pizza_Order {
    public static void main(String[] args) {
        //TASK #5 Create a DecimalFormat object with 2 decimal places
        // You have to add code!!!
        DecimalFormat DollarFormat = new DecimalFormat();
        //Create a Scanner object to read input
        Scanner keyboard = new Scanner(System.in);

        //Create an instance of a Pizza
        Pizza order = new Pizza();

        String firstName;           //user's first name

        boolean discount = false;       //flag,

        //true if user is eligible for discount

        int inches;             //size of the pizza

        char crustType;             //type of crust

        double cost;                //cost of the pizza

        final double TAX_RATE = .08;        //sales tax rate

        double tax;             //amount of tax

        char choice;                //user's choice

        String input;               //user input

        String toppings = "Cheese ";        //list of toppings

        int numberOfToppings = 0;       //number of toppings

        //prompt user and get first name

        System.out.println("Welcome to Mike and Diane's Pizza");

        System.out.print("Enter your first name:  ");

        firstName = keyboard.nextLine();

        //determine if user is eligible for discount by

        //having the same first name as one of the owners

        //TASK #1

        // You have to add code!!!

        if(firstName == "Mike" || firstName == "mike")

        {

            discount = true;
        }

        if(firstName == "Diane" || firstName == "diane")

        {

            discount = true;
        }

        //prompt user and get pizza size choice

        System.out.println("Pizza Size (inches ) Cost");

        System.out.println("                10            $10.99");

        System.out.println("                12            $12.99");

        System.out.println("                14            $14.99");

        System.out.println("                16            $16.99");

        System.out.println("What size pizza would you like?");

        System.out.print("10, 12, 14, or 16 (enter the number only): ");

        inches = keyboard.nextInt();

        //set price and size of pizza ordered

        //ADD LINES HERE FOR TASK #2

        // You have to add code!!!

        if(inches == 10)

        {

            order.setSize(10);

            order.setCost(10.99);
        } else if(inches == 12)

        {

            order.setSize(12);

            order.setCost(12.99);
        } else if(inches == 14)

        {

            order.setSize(14);

            order.setCost(14.99);
        } else if(inches == 16)

        {

            order.setSize(16);

            order.setCost(16.99);
        } else

        {

            order.setSize(12);

            order.setCost(12.99);

            System.out.println("A size other than the available"

                               + " sizes was select, a 12 inche pizza will be made.");
        }

        //consume the remaining newline character

        keyboard.nextLine();

        //prompt user and get crust choice

        System.out.println("What type of crust do you want? ");

        System.out.println("(H)Hand-tossed, (T) Thin-crust, or "

                           + "(D) Deep-dish (enter H, T, or D:): ");

        input = keyboard.nextLine();

        crustType = input.charAt(0);

        //set user's crust choice on pizza ordered

        //ADD LINES FOR TASK #3

        // You have to add code!!!

        switch(crustType)

        {

            case 'H':

            case 'h':

                order.setCrust("Hand-tossed");

                break;

            case 'T':

            case 't':

                order.setCrust("Thin-crust");

                break;

            case 'D':

            case 'd':

                order.setCrust("Deep-dish");

                break;

            default:

                System.out.println("A choice other than the available choices was made,"

                                   + " a hand-tossed pizza will be made.");

                order.setCrust("Hand-tossed");
        }

        //prompt user and get topping choices one at a time

        System.out.println("All pizzas come with cheese.");

        System.out.println("Additional toppings are $1.25 each,"

                           + " choose from");

        System.out.println("Pepperoni, Sausage, Onion, Mushroom");

        //if topping is desired,

        //add to topping list and number of toppings

        System.out.print("Do you want Pepperoni? (Y/N): ");

        input = keyboard.nextLine();

        choice = input.charAt(0);

        if(choice == 'Y' || choice == 'y')

        {

            numberOfToppings += 1;

            toppings = toppings + "Pepperoni ";
        }

        System.out.print("Do you want Sausage? (Y/N): ");

        input = keyboard.nextLine();

        choice = input.charAt(0);

        if(choice == 'Y' || choice == 'y')

        {

            numberOfToppings += 1;

            toppings = toppings + "Sausage ";
        }

        System.out.print("Do you want Onion? (Y/N): ");

        input = keyboard.nextLine();

        choice = input.charAt(0);

        if(choice == 'Y' || choice == 'y')

        {

            numberOfToppings += 1;

            toppings = toppings + "Onion ";
        }

        System.out.print("Do you want Mushroom? (Y/N): ");

        input = keyboard.nextLine();

        choice = input.charAt(0);

        if(choice == 'Y' || choice == 'y')

        {

            numberOfToppings += 1;

            toppings = toppings + "Mushroom ";
        }

        //set number of toppings and topping list on pizza ordered

        order.setNumToppings(numberOfToppings);

        order.setToppingList(toppings);

        //add additional toppings cost to cost of pizza

        order.setCost(1.25 * numberOfToppings);

        //display order confirmation

        System.out.println();

        System.out.println("Your order is as follows: ");

        System.out.println(order.getSize() + " inch pizza");

        System.out.println(order.getCrust() + " crust");

        System.out.println(order.getToppingList());

        //display cost of pizza

        cost = order.getCost();

        //apply discount if user is elibible

        //ADD LINES FOR TASK #4 HERE

        // You have to add code!!!

        if(discount = true)

        {

            System.out.println("You are eligible for a $2.00 discount!!");

            order.setCost(cost - 2);
        }

        //EDIT PROGRAM FOR TASK #5

        //SO ALL MONEY OUTPUT APPEARS WITH 2 DECIMAL PLACES

        System.out.println("The cost of your order is: $" + DollarFormat.format(cost));

        //calculate and display tax and total cost

        tax = cost * TAX_RATE;

        System.out.println("The tax is:  $" + DollarFormat.format(tax));

        System.out.println("The total due is:  $" + DollarFormat.format(tax + cost));

        System.out.println("Your order will be ready" +

                           " for pickup in 30 minutes.");
    }
}

最佳答案

class Pizza {
    public void setCost(float f) { ... }
    // other stuff
}

order.setCost(12.99);

这不会编译,因为 12.99 默认情况下是 double,当您尝试自动将 double 转换为 float 时,编译器不喜欢它。将 setCost 的参数更改为 double,或者在所有需要为 float 的文字后面添加 F,例如

order.setCost(12.99F);

[您确实不应该使用其中任何一个来表示货币值,而应该使用 BigDecimal 来代替,因为 floatdouble 不能准确地表示小数位。但是,如果您使用 double,则可能必须处理数十亿美元的数字才能产生影响。将其放在列表中以便稍后学习。]

关于java - 无法解释的无法编译源代码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22083940/

相关文章:

java - 这对使用泛型有什么影响吗?

java - 为USB端口设置套接字作为TCP双向通信

java - 如何使用正则表达式检查字符串的内容

java - 全局 MySQL 连接还是每个请求一个连接?

java - 错误 : resource android:attr/preserveIconSpacing is private

java - 如何将字符串转换为表示小时、分钟和秒的日期对象

java - 向一点移动时的圆碰撞 react

java - 使用 BeanIO 在单个文件中解析多个对象

java - Servlet作为文件下载,而不是在tomcat上呈现HTML

java - 如何在 fragment 中显示 AlertDialog?