2014-11-03 70 views
0

我知道在方法頭文件中,你不應該用分號結尾;不過,我所有的方法頭文件都顯示相同的錯誤:預期。這是針對頭部的末尾以及兩個參數之間的。我將如何解決這個問題?;方法頭中的預期錯誤

import java.util.Scanner; 

    import java.text.DecimalFormat; 

    // This program will calculate the cost of someone's order at a coffee shop with applied possible discounts and tax 

    public class CoffeeShopWithMethods 
    { 
     public static void main (String [] args) 
     { 

     double cost = 0; 
     double discount = 0; 

     // Scanner allows user to enter values 
     Scanner user_input = new Scanner(System.in); 
     String username; 
     System.out.print("\nEnter your username: "); 
     username = user_input.next(); 
     System.out.print ("\nWelcome to Casey's Classic Coffee, " + username + "! \n"); 

     //call methods 
     displayMenu(); 
     displayOutput(cost, discount, Discounted_cost, tax, Total_cost); 

     System.out.println("\nThank you " + username + "! Have a nice day!"); 
    } 

    //outputs the menu to the screen 
    public static void displayMenu() 
    { 
     System.out.println ("\n\tItem\t\tCost\n\t1. Coffee\t$1.50\n\t2. Latte\t$3.50\n\t3. Cappuccino\t$3.25\n\t4. Espresso\t$2.00"); 
    } 

    //prompts the user to enter item number, returns user input 
    public static int getItemNumber(int number) //error: ; expected 
    { 
     int number; 
     Scanner number = new Scanner(System.in); 
     System.out.print ("\nPlease enter the desired item number: "); 
     number = user_input.nextInt(); 
     return number; 
    } 

    //prompts user to enter quantity, returns user input 
    public static int getQuantity (int quantity) //error: ; expected 
    { 
     System.out.print ("\nPlease enter the quantity: "); 
     quantity = user_input.nextInt(); 
     return quanity; 
    } 

    //takes the item number and quantity and returns the subtotal 
    public static double computeSubTotal (double cost) //error: ; expected 
    { 
     int number = getItemNumber(number); 
     int quantity = getQuantity(quantity); 

     // Used final double in order to make coffee shop items constant 
     final double COFFEE = 1.50; 
     final double LATTE = 3.50; 
     final double CAPPUCCINO = 3.25; 
     final double ESPRESSO = 2.00; 

     double cost = 0; 

     if (number == 1) 
      cost = quantity * COFFEE; 
     else if (number == 2) 
      cost = quantity * LATTE; 
     else if (number == 3) 
      cost = quantity * CAPPUCCINO; 
     else if (number == 4) 
      cost = quantity * ESPRESSO; 
    } 

    //takes the subtotal and returns true if the user earned a discount; otherwise, returns false 
    public static boolean discountCheck (double cost) //error: ; expected 
    { 
     boolean status; 

     if (cost >= 10) 
      { 
      status = true; 
      } 
     else if (cost < 10) 
      { 
      status = false; 
      } 
     return status; 
    } 

    //takes the subtotal and returns the dollar amount of the discount earned by the user 
    public static double computeDiscount (double cost, double discount) //error: ; expected 
    { 
     if (discountCheck() == true) 
     { 
      discount = cost * 0.10; 

     } 
     else if (discountCheck() != true) 
     { 
      discount = 0; 

     } 
     return discount; 

    } 

    //takes the subtotal and the discount amount and returns the price after the discount is applied 
    public static double computePriceAfterDiscount (double cost, double discount) //error: ; expected 
    { 
     double discount = 0; 
     double Discounted_cost = 0; 

     Discounted_cost = cost - discount; 
     return Discounted_cost; 
    } 

    //takes the prices after the discount is applied and tax rate and returns the tax amount 
    public static double computeTax(double Discounted_cost) //error: ; expected 
    { 
     tax = Discounted_cost * 0.07; 
     return tax; 
    } 

    //takes the price after the discount is applied and the tax amount and returns the final total 
    public static double computeTotal(double Discounted_cost, double tax) //says ; expected 
    { 
     Total_cost = Discounted_cost + tax; 
     return Total_cost; 
    } 

    //takes the subtotal, discount amount, price after discount, tax, and final total and displays all the lines of output to the user 
    public static void displayOutput(double cost, double discount, double Discounted_cost, double tax, double Total_cost) //says ; expected at the end of method header 
    { 
     //call methods 
     double cost = computeSubTotal(cost); 
     double discount = computeDiscount(cost, discount); 
     double Discounted_cost = computePriceAfterDiscount(cost, discount); 
     double tax = computeTax(Discounted_cost); 
     double Total_cost = computeTotal(Discounted_cost, tax); 

     System.out.printf ("\nTotal before discount and tax: $%.2f\n ", cost); 

     System.out.printf("\nCalculated discount: $%.2f\n", discount); 
     System.out.printf("\nTotal after special discount: $%.2f\n", Discounted_cost); 

     System.out.printf("\nTax: $%.2f\n", tax); 
     System.out.printf ("\nTotal cost: $%.2f\n", Total_cost); 
    } 
} //error:reached end of the file while parsing 
+1

可能不相關,但是您在'System.im'處有一個輸入錯誤。 – August 2014-11-03 03:47:49

+1

我已將您的代碼剪切並粘貼到一個文件中,而且我沒有看到您看到的錯誤。我收到了很多其他錯誤,主要是關於未定義的名稱。 – ajb 2014-11-03 03:47:54

+0

有太多的編譯錯誤,我甚至無法開始......請發佈代碼編譯! – alfasin 2014-11-03 03:49:58

回答

0

1)您使用的是帶有了聲明變量: 爲如:比較這片段與您的代碼段。

public static double computeTotal(double Discounted_cost, double tax) 
    { 
     double Total_cost = Discounted_cost + tax; 
     return Total_cost; 
    } 

2)您正在調用未定義的方法。 例如: 您打電話給discountCheck(),但您已經這樣定義。 和你沒有在上面的方法使用狀態

public static boolean discountCheck (double cost){ 
     boolean status; 

     if (cost >= 10) 
     { 
      status = true; 
     } 
     else if (cost < 10) 
     { 
      status = false; 
     } 
     return status; 
    } 

應該初始化之前進行初始化局部變量。

3)您正在通過參數聲明已經可用於方法的重複變量。 看到這裏定義的代碼由你:

public static void displayOutput(double cost, double discount, double Discounted_cost, double tax, double Total_cost) 
    { 
     //call methods 
     double cost = computeSubTotal(cost); 
     double discount = computeDiscount(cost, discount); 
     double Discounted_cost = computePriceAfterDiscount(cost, discount); 
     double tax = computeTax(Discounted_cost); 
     double Total_cost = computeTotal(Discounted_cost, tax); 

     System.out.printf ("\nTotal before discount and tax: $%.2f\n ", cost); 

     System.out.printf("\nCalculated discount: $%.2f\n", discount); 
     System.out.printf("\nTotal after special discount: $%.2f\n", Discounted_cost); 

     System.out.printf("\nTax: $%.2f\n", tax); 
     System.out.printf ("\nTotal cost: $%.2f\n", Total_cost); 
    } 
+0

您在代碼中輸入了system.in作爲system.im – 2014-11-03 04:05:38

+0

感謝您的幫助!我已經意識到,我也有一次,我通過了所有的分號錯誤 – mrkrebs 2014-11-03 04:08:17

+0

@mrkrebs你不需要寫代碼,如果你的代碼中的子句 – 2014-11-03 04:15:53

0

我會通過提取您的MenuItem(S)爲enum像開始,

static enum MenuItem { 
    COFFEE("Coffee", 1.5), LATTE("Latte", 3.5), CAPPUCINO("Cappuccino", 
      3.25), ESPRESSO("Espresso", 2); 
    MenuItem(String name, double cost) { 
     this.name = name; 
     this.cost = cost; 
    } 

    double cost; 
    String name; 

    public String toString() { 
     return String.format("%s $%.2f", name, cost); 
    } 
} 

那麼你的編譯器錯誤,主要是由於聲明覆制局部變量名。我是能夠解決的編譯器錯誤而產生的東西,看起來像你想要什麼,

public static void main(String[] args) { 
    Scanner scan = new Scanner(System.in); 
    System.out.println("Enter your username: "); 
    String username = scan.nextLine(); 
    System.out.printf("Welcome to Casey's Classic Coffee, %s!%n", username); 
    displayMenu(); 
    displayOutput(scan); 
    System.out.printf("Thank you %s! Have a nice day!", username); 
} 

// outputs the menu to the screen 
public static void displayMenu() { 
    MenuItem[] items = MenuItem.values(); 
    for (int i = 0; i < items.length; i++) { 
     System.out.printf("%d %s%n", i + 1, items[i]); 
    } 
} 

public static int getItemNumber(Scanner scan) { 
    System.out.println("Please enter the desired item number: "); 
    return scan.nextInt(); 
} 

public static int getQuantity(Scanner scan) { 
    System.out.println("Please enter the quantity: "); 
    return scan.nextInt(); 
} 

public static double computeSubTotal(Scanner scan) { 
    int number = getItemNumber(scan); 
    int quantity = getQuantity(scan); 

    return quantity * MenuItem.values()[number - 1].cost; 
} 

public static boolean discountCheck(double cost) { 
    return (cost >= 10); 
} 

public static double computeDiscount(double cost) { 
    if (discountCheck(cost)) { 
     return cost * 0.10; 
    } 
    return 0; 
} 

public static double computePriceAfterDiscount(double cost, double discount) { 
    return cost - discount; 
} 

public static double computeTax(double Discounted_cost) { 
    return Discounted_cost * 0.07; 
} 

public static double computeTotal(double Discounted_cost, double tax) { 
    return Discounted_cost + tax; 
} 

public static void displayOutput(Scanner scan) { 
    double cost = computeSubTotal(scan); 
    double discount = computeDiscount(cost); 
    double Discounted_cost = computePriceAfterDiscount(cost, discount); 
    double tax = computeTax(Discounted_cost); 
    double Total_cost = computeTotal(Discounted_cost, tax); 

    System.out.printf("Total before discount and tax: $%.2f%n", cost); 

    System.out.printf("Calculated discount: $%.2f%n", discount); 
    System.out.printf("Total after special discount: $%.2f%n", 
      Discounted_cost); 

    System.out.printf("Tax: $%.2f%n", tax); 
    System.out.printf("Total cost: $%.2f%n", Total_cost); 
} 
0

這裏是整個代碼更正和工作:http://ideone.com/ta0R21 我真的建議重新設計這一點。我建議在某些情況下使用全局變量。 就像Scanner對象。而不是初始化每個方法調用的新的Scanner,而是使用全局的 之一來處理整個作業