在做 Java 教程,遇到错误消息。

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

doing java tutorials, getting error messages

问题

以下是翻译好的部分:

我正在进行一个教程,尝试学习基本的 Java。我有 99% 的把握已经按照教程的步骤进行了,但是我收到了一堆这样的错误信息:

C:\Users\krist\IdeaProjects\Hello World\src\Main.java:10:45
java: 预期找到 ')'

C:\Users\krist\IdeaProjects\Hello World\src\Main.java:11:28
java: 不是一个语句

C:\Users\krist\IdeaProjects\Hello World\src\Main.java:11:36
java: 预期找到 ';'

在教程中,他们没有使用这些符号,但是代码在教程中仍然能够正常工作。是否可能 Java 已经有了一些更新,改变了一些东西,或者我可能缺少一些软件来使这个工作起来?

这是 Car.java 的代码:

import java.awt.*;
public class Car {

    // 数据类型:
    // int -> 1, 2, 3...
    // double -> 小数 34.5, 32.1
    // String -> "a1a2" 或 "Hello World"
    // Color -> 来自 awt 库
    // Boolean -> true/false

    double averageMilesPerGallon;
    String licencePlate;
    Color paintColor;
    boolean areTaillightsWorking;

    public Car(double inputAverageMPG,
               String inputLicencePlate,
               Color inputPaintColor,
               boolean inputAreTaillightsWorking) {
        this.averageMilesPerGallon = inputAverageMPG;
        this.licencePlate = inputLicencePlate;
        this.paintColor = inputPaintColor;
        this.areTaillightsWorking = inputAreTaillightsWorking;
    }

    public void changePaintColor(Color newPaintColor) {
        this.paintColor = newPaintColor;
    }
}

这是 main.java 的代码:

import java.awt.*;
import static java.awt.Color.*;

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello World");
        System.out.println("Markus");

        Car myCar = new Car (inputAverageMPG: 25.5,
        inputLicencePlate: "1BC32E",
        inputColor.BLUE,
        inputAreTaillightsWorking: true);

        Car sallyCar = new Car (inputAverageMPG: 13.9,
                inputLicencePlate: "3D20BN",
                inputColor.BLACK,
                inputAreTaillightsWorking: false);

        System.out.println("My Car's licence plate: " +myCar.licencePlate);
        System.out.println("Sally's Car's licence plate: " +sallyCar.licencePlate);

    }
}

如果需要进一步的帮助,请随时提问。

英文:

I am doing a tutorial trying to learn basic java. I'm 99% sure I've followed the tutorial correctly, but I get a bunch of these error messages:

C:\Users\krist\IdeaProjects\Hello World\src\Main.java:10:45
java: ')' expected

C:\Users\krist\IdeaProjects\Hello World\src\Main.java:11:28
java: not a statement

C:\Users\krist\IdeaProjects\Hello World\src\Main.java:11:36
java: ';' expected

In the tutorial, they don't use this symbols, but the code still works in the tutorial.
Could it be there have been some updates in java which changed things, or could I be missing some software to make this work?

Here is the code for Car.java:

import java.awt.*;
public class Car {

    // Data types:
    // int -> 1, 2, 3...
    // double -> decimal 34.5, 32.1
    // String -> "a1a2" or "Hello World"
    // Color -> from awt library
    // Boolean -> true/false

    double averageMilesPerGallon;
    String licencePlate;
    Color paintColor;
    boolean areTaillightsWorking;

    public Car(double inputAverageMPG,
               String inputLicencePlate,
               Color inputPaintColor,
               boolean inputAreTaillightsWorking) {
        this.averageMilesPerGallon = inputAverageMPG;
        this.licencePlate = inputLicencePlate;
        this.paintColor = inputPaintColor;
        this.areTaillightsWorking = inputAreTaillightsWorking;
    }

    public void changePaintColor(Color newPaintColor) {
        this.paintColor = newPaintColor;
    }
}

And here is the code for main.java:

import java.awt.*;
import static java.awt.Color.*;

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello World");
        System.out.println("Markus");

        Car myCar = new Car (inputAverageMPG: 25.5,
        inputLicencePlate: "1BC32E",
        inputColor.BLUE,
        inputAreTaillightsWorking: true);

        Car sallyCar = new Car (inputAverageMPG: 13.9,
                inputLicencePlate: "3D20BN",
                inputColor.BLACK,
                inputAreTaillightsWorking: false);

        System.out.println("My Car's licence plate: " +myCar.licencePlate);
        System.out.println("Sally's Car's licence plate: " +sallyCar.licencePlate);

    }
}

I'm sorry if this question is phrased poorly, I still don't know all the technical terms to explain this in a very good way.

答案1

得分: 1

这里是对构造函数/方法的无效调用:

Car myCar = new Car(inputAverageMPG: 25.5,
inputLicencePlate: "1BC32E",
inputColor: Color.BLUE,
inputAreTaillightsWorking: true);

改为:

Car myCar = new Car(25.5, "1BC32E", ...
英文:

this here is an invalid call to a constructor/method:

Car myCar = new Car(inputAverageMPG: 25.5,
inputLicencePlate: "1BC32E",
inputColor.BLUE,
inputAreTaillightsWorking: true);

instead do:

Car myCar = new Car(25.5, "1BC32E",  ...

答案2

得分: 0

new Car(inputAverageMPG: 25.5,
    inputLicencePlate: "1BC32E",
    inputColor.BLUE,
    inputAreTaillightsWorking: true);

这是不正确的。Java中没有像这样的命名参数。

问题在于你正在使用IntelliJ IDEA,它会给你参数提示。它会类似于这样的效果:

[![屏幕截图][1]][1]

仔细观察,你会发现参数名字的字体不同。

可惜的是,这个功能给新手带来了很多困惑。


<details>
<summary>英文:</summary>

new Car(inputAverageMPG: 25.5,
inputLicencePlate: "1BC32E",
inputColor.BLUE,
inputAreTaillightsWorking: true);


This is not right. Java does not have named parameters like this.

The problem is that you are using IntelliJ IDEA which gives you parameter hints. It would look like something like this:

[![Screenshot][1]][1]

If you look closely, you can see that the parameter names are drawn in another font.

Sadly, this feature brings a lot of confusion to newcomers.


  [1]: https://i.stack.imgur.com/Wqr3w.png

</details>



huangapple
  • 本文由 发表于 2020年9月22日 16:45:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/64006134.html
匿名

发表评论

匿名网友

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

确定