我正在尝试编译以下简单代码并收到错误, 错误类接口或枚举期望 请帮我找出我的错误。
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());
}
答案 0 :(得分:2)
你的main方法是在类外部声明的,并且一个方法不能单独存在。
解决方案:将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());
}
}
提示:对于与此类似的错误,请务必仔细查看导致错误的行,同时仔细查看其上方的一行或两行。
答案 1 :(得分: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());
}