如何修复“非法的表达式起始”错误?

huangapple go评论81阅读模式
英文:

How would I fix "Illegal start of expression"?

问题

我在运行以下代码时遇到了`非法的表达式开始`错误

```java
import java.util.Scanner;

public class ParadiseInfo2 {

    public static void main(String[] args) {
        double price;
        double discount;
        double savings;
        Scanner keyboard = new Scanner(System.in);
        System.out.print("输入折扣的价格阈值 >> ");
        price = keyboard.nextDouble();
        System.out.print("输入折扣率作为整数 >> ");
        discount = keyboard.nextDouble();
        displayInfo();
    }

    public static double computeDiscountInfo(double price, double discountRate) {
        double savings;
        savings = price * discountRate / 100;
        return savings;
    }

    public static void displayInfo() {
        System.out.println("Paradise Day Spa希望照顾您。");
        System.out.println("我们会让您看起来很好。");
        System.out.println("本周特价,任何超过" + price + "的服务");
        System.out.println("折扣为" + discount + "百分比");
        System.out.println("至少节省了" + savings + "美元");
    }
}

我做错了什么?

英文:

I am getting Illegal start of expression when I run the following code:

import java.util.Scanner;

public class ParadiseInfo2 {

    public static void main(String[] args) {
        double price;
        double discount;
        double savings;
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter cutoff price for discount >> ");
        price = keyboard.nextDouble();
        System.out.print("Enter discount rate as a whole number >> ");
        discount = keyboard.nextDouble();
        displayInfo();

        public static double computeDiscountInfo(double price, double discountRate) {
            double savings;
            savings = price * discountRate / 100;
            return savings;
        }
        savings = computeDiscountInfo(price, discount);
    }

    public static void displayInfo() {
        System.out.println("Paradise Day Spa wants to pamper you.");
        System.out.println("We will make you look good.");
        System.out.println("Special this week on any service over" + price);
        System.out.println("Discount of " + discount + "percent");
        System.out.println("That's a savings of at least $" + savings);
    }
}

What am I doing wrong?

答案1

得分: 0

不允许的部分:

    public static void main(String[] args) {
        double price;
        double discount;
        double savings;
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter cutoff price for discount >> ");
        price = keyboard.nextDouble();
        System.out.print("Enter discount rate as a whole number >> ");
        discount = keyboard.nextDouble();
        displayInfo();
            // ***  This method can not be here!
            public static double computeDiscountInfo(double price, double discountRate) {
                double savings;
                savings = price * discountRate / 100;
                return saings;
            }
            // ***
        savings = computeDiscountInfo(price, discount);
    }

允许的部分:

    public static void main(String[] args) {
        double price;
        double discount;
        double savings;
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter cutoff price for discount >> ");
        price = keyboard.nextDouble();
        System.out.print("Enter discount rate as a whole number >> ");
        discount = keyboard.nextDouble();
        displayInfo();
        savings = computeDiscountInfo(price, discount);
    }

    public static double computeDiscountInfo(double price, double discountRate) {
        double savings;
        savings = price * discountRate / 100;
        return saings;
    }

编辑:根据您的评论,您的另一个问题如下:

根据您的评论,您遇到了这个问题,因为变量 pricediscountsavingsdisplayInfo() 方法中不可用。它们超出了范围。在这里有两件事情可以做。将这些变量作为参数添加到该方法中:

public static void displayInfo(double price, double discount, double savings) {
    System.out.println("Paradise Day Spa wants to pamper you.");
    System.out.println("We will make you look good.");
    System.out.println("Special this week on any service over" + price);
    System.out.println("Discount of " + discount + "percent");
    System.out.println("That's a savings of at least $" + savings);
}

或者,由于您在所有类方法中都使用了这些变量,将这些变量设置为类成员变量,使得作用域在整个类中是 全局 的,例如:

public class MyClassWhateverItsCalled {

    private static double price = 0.0d;
    private static double discount = 0.0d;
    private static double savings  = 0.0d;

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter cutoff price for discount >> ");
        this.price = keyboard.nextDouble();
        System.out.print("Enter discount rate as a whole number >> ");
        this.discount = keyboard.nextDouble();
        this.savings = computeDiscountInfo();
        displayInfo();
    }

    public static void computeDiscountInfo() {
        this.savings = this.price * this.discountRate / 100;
    }

    public static void displayInfo() {
        System.out.println("Paradise Day Spa wants to pamper you.");
        System.out.println("We will make you look good.");
        System.out.println("Special this week on any service over $" + this.price);
        System.out.println("Discount of " + this.discount + "%");
        System.out.println("That's a savings of at least $" + this.savings);
    }
}

类似这样...

为了消除一切都需要是静态的需要,您可以这样做(main() 必须保持静态):

public class MyClassWhateverItsCalled {

    private double price = 0.0d;
    private double discount = 0.0d;
    private double savings  = 0.0d;

    public static void main(String[] args) {
        new MyClassWhateverItsCalled().startApp(args);
    }

    private void startApp(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter cutoff price for discount >> ");
        this.price = keyboard.nextDouble();
        System.out.print("Enter discount rate as a whole number >> ");
        this.discount = keyboard.nextDouble();
        this.savings = computeDiscountInfo();
        displayInfo();       
    } 

    public void computeDiscountInfo() {
        this.savings = this.price * this.discountRate / 100;
    }

    public void displayInfo() {
        System.out.println("Paradise Day Spa wants to pamper you.");
        System.out.println("We will make you look good.");
        System.out.println("Special this week on any service over $" + this.price);
        System.out.println("Discount of " + this.discount + "%");
        System.out.println("That's a savings of at least $" + this.savings);
    }
}

类似这样...

英文:

Not Allowed:

public static void main(String[] args) {
double price;
double discount;
double savings;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter cutoff price for discount >> ");
price = keyboard.nextDouble();
System.out.print("Enter discount rate as a whole number >> ");
discount = keyboard.nextDouble();
displayInfo();
// ***  This method can not be here!
public static double computeDiscountInfo(double price, double discountRate) {
double savings;
savings = price * discountRate / 100;
return saings;
}
// ***
savings = computeDiscountInfo(price, discount);
}

This is allowed:

public static void main(String[] args) {
double price;
double discount;
double savings;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter cutoff price for discount >> ");
price = keyboard.nextDouble();
System.out.print("Enter discount rate as a whole number >> ");
discount = keyboard.nextDouble();
displayInfo();
savings = computeDiscountInfo(price, discount);
}
public static double computeDiscountInfo(double price, double discountRate) {
double savings;
savings = price * discountRate / 100;
return saings;
}

**

> EDIT: Your other problem according to you comments:

As per your comments, you are having this issue because the variables price, discount, and savings are not available to the displayInfo() method. They are out of scope. One of two things you can do here. Add these variables as parameters to that method:

public static void displayInfo(double price, double discount, double savings) {
System.out.println("Paradise Day Spa wants to pamper you.");
System.out.println("We will make you look good.");
System.out.println("Special this week on any service over" + price);
System.out.println("Discount of " + discount + "percent");
System.out.println("That's a savings of at least $" + savings);
}

OR since you utilize these variables in all your class methods, make those variables Class Member Variables so that scope will be global to the entire class, for example:

public class MyClassWhateverItsCalled {
private static double price = 0.0d;
private static double discount = 0.0d;
private static double savings  = 0.0d;
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter cutoff price for discount >> ");
this.price = keyboard.nextDouble();
System.out.print("Enter discount rate as a whole number >> ");
this.discount = keyboard.nextDouble();
this.savings = computeDiscountInfo();
displayInfo();
}
public static void computeDiscountInfo() {
this.savings = this.price * this.discountRate / 100;
}
public static void displayInfo() {
System.out.println("Paradise Day Spa wants to pamper you.");
System.out.println("We will make you look good.");
System.out.println("Special this week on any service over $" + this.price);
System.out.println("Discount of " + this.discount + "%");
System.out.println("That's a savings of at least $" + this.savings);
}
}

Something like that....

To eliminate the need for everything to be static you can do this (main() needs to remain static):

public class MyClassWhateverItsCalled {
private double price = 0.0d;
private double discount = 0.0d;
private double savings  = 0.0d;
public static void main(String[] args) {
new MyClassWhateverItsCalled().startApp(args);
}
private void startApp(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter cutoff price for discount >> ");
this.price = keyboard.nextDouble();
System.out.print("Enter discount rate as a whole number >> ");
this.discount = keyboard.nextDouble();
this.savings = computeDiscountInfo();
displayInfo();       
} 
public void computeDiscountInfo() {
this.savings = this.price * this.discountRate / 100;
}
public void displayInfo() {
System.out.println("Paradise Day Spa wants to pamper you.");
System.out.println("We will make you look good.");
System.out.println("Special this week on any service over $" + this.price);
System.out.println("Discount of " + this.discount + "%");
System.out.println("That's a savings of at least $" + this.savings);
}
}

huangapple
  • 本文由 发表于 2020年10月16日 01:38:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64376963.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定