2014-02-28 1 views
1

아래 프로그램을 실행하려고해도 오류가 계속 발생합니다. 나는 어디서 잘못되었는지 확신하지 못한다. 어떤 도움이라도 대단히 감사하겠습니다.Unexplained Uncompilable 소스 코드 오류

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 

자바 결과 : 1 BUILD SUCCESSFUL (총 시간 : 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.99는 기본적으로 double 때문에이 컴파일되지 않으며, 컴파일러는 '아무튼 doublefloat으로 자동 변환하려고하면 좋아요. setCost의 매개 변수를 double으로 변경하거나 일 필요가있는 모든 리터럴 뒤에 F을 넣습니다.

order.setCost(12.99F); 

[당신은 정말 화폐 가치를 나타내는 중 하나를 사용되어서는 안되며, 오히려 BigDecimal 대신, floatdouble 이후 정확히 소수점 이하 자릿수를 대표하지 않습니다. 그러나 double을 사용하는 경우 차이가 나기 전에 수십억 달러의 숫자로 작업해야 할 것입니다. 그 목록에 나중에 배울 점이 있으십시오.]

+1

오오오! 내 마음이 날아 갔다. 고마워요. 많은 도움이되었습니다. – PowerofMerlin

+0

사실, 간단한 금융 프로그래밍을 위해 BigDecimal이 필요하지 않습니다. 현금을 분수 달러가 아닌 동전으로 나타내면 부동 소수점 또는 BigDecimal의 복잡성이나 비용이 들지 않는 정수를 사용할 수 있습니다. "고정 소수점 숫자 (fixed-point numbers)"는 인쇄용으로 다시 포맷팅 된 int를 의미하며, 대안 중 하나보다 빠르고 간단하고 저렴합니다. – keshlam

+0

@keshlam 맞습니다. 직접 조정 해주십시오. Ada 언어에는 숫자가 정수로 표시되지만 컴파일러는 축척 비율을 추적하는 고정 소수점 숫자가 내장되어 있습니다. 아직 완료되지 않은 경우 Java에서 동일한 작업을 수행하도록 클래스를 고안해서는 안됩니다. – ajb

관련 문제