错误:预期的类,委托,枚举,接口或结构

时间:2013-06-27 17:22:56

标签: c#

修复了代码,但最后仍显示错误。 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);  
    }

}

5 个答案:

答案 0 :(得分: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.

答案 1 :(得分:2)

删除末尾的分号;

public Character(string char_name, string char_desc, byte char_level, 
byte char_attack, 
byte char_defence, bool char_defeat);   //<< - remove the semicolon 

答案 2 :(得分: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)  
{  
   ...  
}

注意删除分号;

答案 3 :(得分:0)

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

答案 4 :(得分:0)

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