0

修复了代码,最后仍然显示错误。在哪里 public void displayCharacterDetails()

public class Character
{
    private string char_name;
    private string char_descr;
    private byte char_level;
    private byte char_attack;
    private byte char_defence;
    private bool char_defeat;

    public Character(string name, string desc, byte level, byte attack, byte defence, bool defeat)
    {
        char_name = name;
        char_descr = "";
        char_level = level;
        char_attack = attack;
        char_defence = defence;
        char_defeat = defeat;
    }

    public string GetCharacterName()
    {
        return char_name;
    }

    public string GetCharacterDescription()
    {
        return char_descr;
    }

    public void SetCharacterDescription(string descr)
    {
        char_descr = descr;
    }

    public byte GetCharLevel()
    {
        return char_level;
    }

    public byte GetCharacterAttack()
    {
        return char_attack;
    }



    public byte GetCharacterDefence()
    {

        return char_defence;

    }

    public void resetCharacter(string name, string descr, byte level, byte attack, byte defence, bool defeat)
    {
        char_name = name;
        char_descr = "";
        char_level = level;
        char_attack = attack;
        char_defence = defence;
        char_defeat = defeat;
    }

    public void displayCharacterDetails() 

    { 

System.out.println("Character name: " + char_name); 

System.out.println("Character description" + char_descr); 

System.out.println("Character Level, Attack, Defence: " + char_level + " ,"+ char_attack + "  ,"+ char_defence);  
    }

}
4

5 回答 5

2

将第一行替换为:

public Character(string char_name, string char_desc, byte char_level, byte char_attack, byte char_defence, bool char_defeat)

;从方法声明中删除。

此外,您在这里缺少一个右括号:

public byte getCharacterAttack() 

{ 

而且,您不能将方法命名为私有字段。

您有一个名为的字段char_level(如在您的构造函数中看到的this.char_level)和一个具有相同名称的方法:

public byte char_level() 

{ 

return char_level; 

}  

重命名该字段或解决该问题的方法。

最后,在您的构造函数中,您有名为 lkike 的参数,byte char_level但您有this.char_level = level;。将其替换this.char_level = char_level;为每个参数,或仅调用每个参数byte level


所以基本上你应该有这个:

public class Character
{
    private string char_name;
    private string char_descr;
    private byte char_level;
    private byte char_attack;
    private byte char_defence;
    private bool char_defeat;

    public Character(string name, string desc, byte level, byte attack, byte defence,
                     bool defeat)
    {
        char_name = name;
        char_descr = "";
        char_level = level;
        char_attack = attack;
        char_defence = defence;
        char_defeat = defeat;
    }

    public string GetCharacterName()
    {
        return char_name;
    }

    public string GetCharacterDescription()
    {
        return char_descr;
    }

    public void SetCharacterDescription(string descr)
    {
        char_descr = descr;
    }

    public byte GetCharLevel()
    {
        return char_level;
    }

    public byte GetCharacterAttack()
    {
        return char_attack;
    }
}

我重命名了您的方法以遵循 C# 约定。我还删除了this关键字,那些参数名称不需要它。另外请避免在您的留置权之间放置太多空格,这样会很难阅读。


或者您可以简单地使用 C# 属性:

public class Character
{
    public string Name { get; set; }
    public string Descr { get; set; }
    public byte Level { get; set; }
    public byte Attack { get; set; }
    public byte Defence { get; set; }
    public bool Defeat { get; set; }
{

并像这样创建新角色:

Character character = new Character { Name = "the name", Descr = "the descr" } //etc.
于 2013-06-27T17:25:15.387 回答
2

去掉最后的分号;

public Character(string char_name, string char_desc, byte char_level, 
byte char_attack, 
byte char_defence, bool char_defeat);   //<< - remove the semicolon 
于 2013-06-27T17:25:28.070 回答
0
public Character(string char_name, string char_desc, byte char_level,  
     byte char_attack, byte char_defence, bool char_defeat);  

不是你如何声明一个构造函数。你要这个:

public Character(string char_name, string char_desc, byte char_level,  
     byte char_attack, byte char_defence, bool char_defeat)  
{  
   ...  
}

注意分号的删除;

于 2013-06-27T17:26:48.997 回答
0

您的分配向后看,如果您传入一个名为 char_name 的参数,并且看起来您有一个名为 name 的类字段,则构造函数中的代码行应该是 this.name = char_name; 您的 set 方法中存在相同的问题。

于 2013-06-27T17:27:13.983 回答
0

你在构造函数后面加了一个分号。

于 2013-06-27T17:27:46.070 回答