Java基本编译错误

use*_*186 0 java compiler-errors

我有一个简单的错误,在一个非常简单的程序开始让我难过.我在相应的行中包含了错误的注释.我错过了什么?(是的,我是个笨蛋.)

package mainPack;

public class Bodies{

    int mass;
    int radius;
    int xpos;
    int ypos;
    float xvel;
    float yvel;   //Syntax error, insert "}" to complete ClassBody

    public Bodies(mass, radius, xpos, ypos, xvel, yvel){
    }

}   //Syntax error on token "}", delete this token
Run Code Online (Sandbox Code Playgroud)

Ste*_* P. 6

您的问题是构造函数中的参数没有数据类型.

注意:
由于您的参数名称与实例变量名称相同,因此您需要使用this,如:

   public Bodies(int mass, int radius, int xpos, int ypos, float xvel, float yvel)
    {
        this.mass = mass;
        this.radius = radius;
        //...
    }
Run Code Online (Sandbox Code Playgroud)

where this.mass指的是实例变量mass,而不是构造函数的传递参数.

有关更多信息,请查看Java Constructors上Oracle教程.

另外,float也来自Oracle:

与byte和short的建议一样,如果需要在大型浮点数数组中保存内存,请使用float(而不是double).绝不应将此数据类型用于精确值,例如货币.