2013-11-29 194 views
-6
public void giveTitle;int titleId; 
      { 
     this.playerTitle = titleId; 
     this.setAppearanceUpdateRequired(true); 
    } 

当我编译它时,我的编译器说“public”和“void”都是表达式的非法开始。所有的帮助将不胜感激!如果我需要澄清更多请问!我做错了什么?

+2

它应该是'giveTitle()'用'()' – JoelFernandes

+0

你想做什么?试图宣布一种方法?或者一些变量后跟一段代码? – rajneesh2k10

回答

1

方法的参数都被放在括号:

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

应该是这样的:

public void giveTitle(int titleId) 
      { 
     this.playerTitle = titleId; 
     this.setAppearanceUpdateRequired(true); 
    } 
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 
+0

好的解释。 – MouseLearnJava