分号预期方法标题中的错误

时间:2014-11-03 03:41:52

标签: java methods compiler-errors static-methods

我知道在方法标题中你不应该以分号结尾;但是,我的所有方法标题都显示相同的错误:;预期。这是针对标题的结尾以及两个参数之间的结尾。我该如何解决这个问题?

    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

3 个答案:

答案 0 :(得分: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);
    }

答案 1 :(得分: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);
}

答案 2 :(得分:0)

以下是您的整个代码已更正并正常运行:http://ideone.com/ta0R21 我真的建议重新设计这个。我建议在某些情况下使用全局变量。 像Scanner对象一样。不是初始化每个方法调用的新Scanner,而是使用全局 一个人来处理整个工作