0

我正在尝试编译以下简单代码并收到错误,错误类接口或枚举预期请帮助我找出我的错误。

public class Scores
{ 
    public static final int maxEntries =5; 

    int numEntries; 
    protected GameEntry[] entries; 

    public Scores() 
    {
        entries = new GameEntry[maxEntries];
        numEntries = 0;
    }

    public String toString()
    {
        String s = "[";
        for (int i=0 ; i < numEntries; i++)
        {
            if (i > 0) s = ","; // separate entries by commas   
            s = s + entries[i].toString() ;
        }
        return s + "]";
    }

    public void add(GameEntry e) 
    {
        entries[numEntries]=e;
        numEntries++;
    }

    public void add(GameEntry e)
    {
        int newScore = e.getScore();
        if (numEntries == maxEntries) 
        { 
            if (newScore <= entries[numEntries-l].getScore())
            return;
        }
        else
        return numEntries++;

        int i = numEntries-l;
        for ( ; (i >= 1) && (newScore > entries[i-1].getScore()); i--)
        entries[i] = entries[i-1]; 
        entries[i] = e; 
    }
}

    public static  void main(String args[])
    {
    Scores s1=new Scores();
            GameEntry e1=new GameEntry("Paul",750);
        s1.add(e1);

        GameEntry e2=new GameEntry("John",770); 
        s1.add(e2); 

        system.out.println(s1.toString());

        GameEntry e1=new GameEntry("Sam",600);
        s1.add(e1);

        GameEntry e2=new GameEntry("Raj",800);  
        s1.add(e2);


        system.out.println(s1.toString());

        GameEntry e1=new GameEntry("Gobi",550);
        s1.add(e1);

        GameEntry e2=new GameEntry("Raj",580);  
        s1.add(e2);


        system.out.println(s1.toString());

    }
4

2 回答 2

2

@Hovercraft Full Of Eels是对的。否则,您不能在一个范围内多次定义一个变量名。

像这样更改您的主存根:

public static void main(String args[])
{
    Scores s1 = new Scores();

    GameEntry e1 = new GameEntry("Paul", 750);
    s1.add(e1);

    GameEntry e2 = new GameEntry("John", 770); 
    s1.add(e2); 

    system.out.println(s1.toString());

    e1 = new GameEntry("Sam", 600);
    s1.add(e1);

    e2 = new GameEntry("Raj", 800);  
    s1.add(e2);

    system.out.println(s1.toString());

    e1 = new GameEntry("Gobi", 550);
    s1.add(e1);

    e2 = new GameEntry("Raj", 580);  
    s1.add(e2);

    system.out.println(s1.toString());

}
于 2013-08-26T23:40:24.693 回答
2

您的主要方法是在类之外自行声明的,并且方法不能单独存在。

解决方案:将 main 方法移动到类体中,以便您的代码进行编译。

public class Scores
{ 
    public static final int maxEntries =5; 

    int numEntries; 
    protected GameEntry[] entries; 

    public Scores() 
    {
        entries = new GameEntry[maxEntries];
        numEntries = 0;
    }

    public String toString()
    {
        String s = "[";
        for (int i=0 ; i < numEntries; i++)
        {
            if (i > 0) s = ","; // separate entries by commas   
            s = s + entries[i].toString() ;
        }
        return s + "]";
    }

    public void add(GameEntry e) 
    {
        entries[numEntries]=e;
        numEntries++;
    }

    public void add(GameEntry e)
    {
        int newScore = e.getScore();
        if (numEntries == maxEntries) 
        { 
            if (newScore <= entries[numEntries-l].getScore())
            return;
        }
        else
        return numEntries++;

        int i = numEntries-l;
        for ( ; (i >= 1) && (newScore > entries[i-1].getScore()); i--)
        entries[i] = entries[i-1]; 
        entries[i] = e; 
    }

    public static  void main(String args[])
    {
        Scores s1=new Scores();
        GameEntry e1=new GameEntry("Paul",750);
        s1.add(e1);

        GameEntry e2=new GameEntry("John",770); 
        s1.add(e2); 

        system.out.println(s1.toString());

        GameEntry e1=new GameEntry("Sam",600);
        s1.add(e1);

        GameEntry e2=new GameEntry("Raj",800);  
        s1.add(e2);


        system.out.println(s1.toString());

        GameEntry e1=new GameEntry("Gobi",550);
        s1.add(e1);

        GameEntry e2=new GameEntry("Raj",580);  
        s1.add(e2);


        system.out.println(s1.toString());

    }
}

提示:对于与此类似的错误,请务必仔细查看导致错误的行,但也要仔细查看其上方的一两行

于 2013-08-26T23:33:08.923 回答