创建实例Java时Eclipse中的错误

时间:2018-04-04 01:16:40

标签: java eclipse

我在Lynda.com上在线学习Java,我们应该做的是创建一个Car Object。以下是Car类的代码:

import java.awt.*; // abstract window toolkit library


public class Car {

double avgMilesPerGallon;
String licensePlate;
Color paintColor;
boolean lightsWorking;

// Constructor
    public Car(double inputAverageMPG, String inputLicensePlate, 
            Color inputPaintColor, boolean inputTaillightsWorking ) {
        this.avgMilesPerGallon = inputAverageMPG;
        this.licensePlate = inputLicensePlate;
        this.paintColor = inputPaintColor;
        this.lightsWorking = inputTaillightsWorking;

    } 

    }

这是我的Main类的代码(我把它命名为CrimsonPeak):

import java.awt.*;

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



     Car myCar = new Car( inputAverageMPG: 25.5, inputLicensePlate: "1BC32E",
             inputPaintColor: Color.BLUE, inputTaillightsWorking: true);

     Car erikaCar = new Car( inputAverageMPG: 13.9, inputLicensePlate: "3D20CE", 
             Color.BLACK, inputTaillightsWorking: false);




 }

}

我正在关注Lynda.com上的视频向我展示的所有内容,但我仍然遇到以下错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
inputAverageMPG cannot be resolved to a variable
Syntax error on token ":", invalid AssignmentOperator
inputLicensePlate cannot be resolved to a variable
Syntax error on token ":", invalid AssignmentOperator
inputPaintColor cannot be resolved
Syntax error on token ":", :: expected
inputTaillightsWorking cannot be resolved to a variable
Syntax error on token ":", invalid AssignmentOperator
inputAverageMPG cannot be resolved to a variable
Syntax error on token ":", invalid AssignmentOperator
inputLicensePlate cannot be resolved to a variable
Syntax error on token ":", invalid AssignmentOperator
Color cannot be resolved to a variable
inputTaillightsWorking cannot be resolved to a variable
Syntax error on token ":", invalid AssignmentOperator
at CrimsonPeak.main(CrimsonPeak.java:11)

我是否知道如何解决上述错误?我试图从Lynda.com的视频中理解它,但我似乎无法找到导致错误的原因。

此外,Lynda.com上的视频展示了上述代码在Intellij IDEA 2017上的演练。这也是我在Eclipse上遇到上述错误的原因吗?

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

创建汽车时,您不必执行inputAverageMPG: 25.5, inputLicensePlate: "1BC32E",依此类推。它只是25.5,"1BC32E"。删除构造函数中的inputAverageMPG:inputLicensePlate:和其他任何位置。

Car myCar = new Car(25.5,"1BC32E", Color.BLUE, true);

Car erikaCar = new Car(13.9, "3D20CE", Color.BLACK, false);

看起来应该是这样的。如果Lynda.com上的教程告诉你把它们放在那里,找一个不同的网站。