【发布时间】:2014-07-03 20:40:10
【问题描述】:
我基本上是在提炼、完成和尝试编译 Java 初学者参考书中的测试代码。目标是创建一个猜谜游戏,其中目标位于 3 个连续的单元格中(我将位置保存在一个数组中)并且用户猜测单元格编号。逐个破坏目标细胞。
我在这里查看了六篇关于相同错误的帖子,但我无法弄清楚出了什么问题。
这是我的错误:
test.java:5: error: illegal start of expression
public int[] locations={1,2,3};
^
1 error
我的代码是:
public class test{
public static void main(String[] args){
test dot=new test();
public int[] locations={1,2,3};
dot.setLocationCells(locations);
String userGuess="2";
String result = dot.checkYourself(userGuess);
String testResult="failed";
if(result.equals("hit")){
testResult="passed";
}
System.out.println(testResult);
}
public String checkYourself(String stringGuess){
int guess=Integer.parseInt(stringGuess);
String result="miss";
int numOfHits=0;
for(int cell:locations){
if(guess==cell){
result="hit";
numOfHits++;
break;
}
}
if(numOfHits==locations.length){
result="kill";
}
System.out.println(result);
return result;
}
public void setLocationCells( int[] locations){
int[] locns;
locns=locations;
}
}
【问题讨论】:
-
有时注释有问题,例如@Import{xxx,xxx,} 在右括号之前有一个额外的 ,
标签: java compiler-errors