无法获得一种基本的工作方法

时间:2018-04-25 08:47:50

标签: java class methods

有人可以告诉为什么getSpeed方法不起作用吗?

每当我将鼠标悬停在方法上时,我都会得到:

插入;
非法修饰语 语法错误请插入[] 令牌

上的语法错误
for

2 个答案:

答案 0 :(得分:4)

您的main方法包含以下方法。

 public double getSpeed() {
        return Speed;
    }

请将此移至Tanks课程。

以下应该工作:

public class Tanks {

private String TankName;
private int TankModel;
private int CrewNumber;
private double Speed;
private int TurretSpeed;

Tanks(String name, int model, int crew, double speed, int turretspeed) {

    this.TankName = name;
    this.TankModel = model;
    this.CrewNumber = crew;
    this.Speed = speed;
    this.TurretSpeed = turretspeed;

}

public double getSpeed() {
    return Speed;
}

public static void main(String[] args) {

    Tanks merkava = new Tanks("MERKAVA", 1, 5, 56.64, 67);
    Tanks judge = new Tanks("JUDGE", 2, 6, 66.66, 68);

    System.out.println(merkava.TankName + " " + merkava.TankModel + " " + merkava.CrewNumber + " " + merkava.Speed
            + " " + merkava.TurretSpeed);
    System.out.println(judge.TankName + " " + judge.TankModel + " " + judge.CrewNumber + " " + judge.Speed + " "
            + judge.TurretSpeed);

}
}

答案 1 :(得分:1)

如果您已经完成了一些基本的Java课程,您将会知道通过在另一个函数(getSpeed())中定义一个方法(在本例中为main)是错误的。