尝试运行以下程序但不断收到错误。我不知道我哪里出错了。非常感谢任何帮助。
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.");
}
}
答案 0 :(得分:1)
class Pizza {
public void setCost(float f) { ... }
// other stuff
}
order.setCost(12.99);
这将无法编译,因为默认情况下12.99
是double
,当您尝试自动将double
转换为{{1}时编译器不喜欢它}}。要么将float
的参数更改为setCost
,要么在所有需要double
的文字之后添加F
,例如
float
[你真的不应该使用任何一个代表货币价值,而是使用order.setCost(12.99F);
,因为BigDecimal
和float
不会完全代表小数位。但是,如果您使用的是double
,那么您可能必须使用数十亿美元的数字才能发挥作用。把它放在列表中,以便稍后学习。]