我做错了什么?

时间:2013-11-29 01:47:10

标签: java

 public void giveTitle;int titleId;
          {
        this.playerTitle = titleId;
        this.setAppearanceUpdateRequired(true);
    }

在我编译它时,我的编译器说“公共”和“无效”都是表达式的非法启动。所有帮助将不胜感激!如果我需要澄清更多请问!

3 个答案:

答案 0 :(得分:3)

打破它:

public void giveTitle;int titleId;
{
    this.playerTitle = titleId;
    this.setAppearanceUpdateRequired(true);
}

就像说这个

public void giveTitle;  // declare giveTitle
int titleId;            // declare titleId

{  // brackets have no effect in this case.
    this.playerTitle = titleId;     // does not work because titleId has not 
                                    // been initialized
    this.setAppearanceUpdateRequired(true);    // 
}

正如您所看到的,您在上面所做的只是声明两个变量,这不是您想要做的。你想创建一个方法。但是根据您所做的public void giveTitle,您收到错误是因为您无法使用public void声明变量

你想要的是这个

public void giveTitle(int titleId)
{                                    // here the brackets encasulate what's inside
    this.playerTitle = titleId;      // making them "belong" to the method.
    this.setAppearanceUpdateRequired(true);
}

只需添加此括号,您就可以将其转换为类似

的方法
public void giveTitle(int titleId)

// a method named giveTitle with a parameter of an int titleId, will set this.title
// to the titleId passed into this method, also set the appearanceUpdateRequired to true

答案 1 :(得分:1)

方法的参数必须放在括号中:

public void giveTitle(int titleId) { .... }

答案 2 :(得分:1)

应该是这样的:

public void giveTitle(int titleId)
          {
        this.playerTitle = titleId;
        this.setAppearanceUpdateRequired(true);
    }