【问题标题】:Semicolon expected error in method header方法头中的分号预期错误
【发布时间】:2014-11-03 03:41:52
【问题描述】:

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

    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

【问题讨论】:

  • 可能不相关,但您在System.im 有错字。
  • 我已将您的代码剪切并粘贴到一个文件中,但我没有收到您所看到的错误。我收到很多其他错误,主要是关于未定义的名称。
  • 编译错误太多了,我什至无法开始......请发布一个可以编译的代码!
  • @alfasin 他无法弄清楚为什么代码无法编译。如果他发布编译的代码,他就不会有问题了!
  • 大部分错误是因为他试图重新定义现有变量。

标签: java methods compiler-errors static-methods


【解决方案1】:

1)您正在使用未声明的变量: 例如:将此 sn-p 与您的代码 sn-p 进行比较。

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);
    }

【讨论】:

  • 您在代码中输入了 system.in 作为 system.im
  • 感谢您的帮助!一旦我克服了所有分号错误,我也意识到了这一点
  • @mrkrebs 您在代码中不必要地编写 else if 子句
【解决方案2】:

我首先将您的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);
}

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-06
      • 2015-10-14
      相关资源
      最近更新 更多