2014-02-28 113 views
1

試圖運行下面的程序,但不斷收到錯誤。我不確定我出錯的地方。任何援助將不勝感激。原因不明的源代碼錯誤

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."); 
    } 
} 
+1

請格式化您的代碼。 –

+4

並消除行號。 – aliteralmind

+1

這與您當前的問題無關,但您應該閱讀[如何比較Java中的字符串?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java ) – PakkuDon

回答

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

order.setCost(12.99); 

這不會編譯,因爲12.99double通過默認,並且編譯器沒有按」當你試圖自動將double轉換爲float時,t喜歡它。將​​的參數更改爲double,或將F更改爲所有需要爲float的文字,例如

order.setCost(12.99F); 

[你真的不應該使用任何一個來代表貨幣價值,而是BigDecimal相反,因爲floatdouble不會完全代表小數。但是,如果您使用的是double,那麼您可能必須使用數十億美元的數字才能發揮作用。把它放在名單上,稍後再學習。]

+1

Ohhhh!我的頭腦剛剛被炸燬。謝謝你幫助我噓! – PowerofMerlin

+0

實際上,對於大多數簡單的財務編程,您都不需要BigDecimal。將您的現金表示爲便士而不是分數美元,並且您可以使用整數 - 它們不具有任何浮點或BigDecimal的複雜性或成本。 「定點數字」意味着僅針對打印進行重新格式化的整版,比任何一種方案都更簡單,更快速,更便宜。 – keshlam

+0

@keshlam對,你只需要記住自己調整它。 Ada語言具有內置的定點數字,其中數字表示爲整數,但編譯器會跟蹤比例因子。如果這還沒有完成,那麼在Java中設計一個類來做同樣的事情應該不難。 – ajb