Java错误:表达式的非法开始
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24562950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Error: illegal start of expression
提问by AnujaPL
I'm basically refining, completing and trying to compile a test code from a reference book for java beginners. The objective is to create a guessing game wherein the target is located in 3 continuous cells (I'm holding the locations in an array) and the user guesses the cell no. to destroy the target cell by cell.
我基本上是在完善、完成并尝试从 Java 初学者的参考书中编译测试代码。目标是创建一个猜谜游戏,其中目标位于 3 个连续的单元格中(我将位置保存在一个数组中),并且用户猜测单元格编号。逐个破坏目标细胞。
I checked out half a dozen posts here on the same error, but I couldn't figure out what was going wrong.
我在这里查看了六篇关于相同错误的帖子,但我无法弄清楚出了什么问题。
This is my error:
这是我的错误:
test.java:5: error: illegal start of expression
public int[] locations={1,2,3};
^
1 error
and my code is:
我的代码是:
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;
}
}
采纳答案by Damian Leszczyński - Vash
Methods can only declare local variables. That is why the compiler reports an error when you try to declare it as public.
方法只能声明局部变量。这就是为什么当您尝试将其声明为 public 时编译器会报告错误的原因。
In the case of local variables you can not use any kind of accessor (public, protected or private).
对于局部变量,您不能使用任何类型的访问器(公共、受保护或私有)。
You should also know what the static keyword means. In method checkYourself
, you use the Integer array locations
.
您还应该知道 static 关键字的含义。在方法中checkYourself
,您使用整数数组locations
。
The static keyword distinct the elements that are accessible with object creation. Therefore they are not part of the object itself.
static 关键字区分了可通过对象创建访问的元素。因此,它们不是对象本身的一部分。
public class Test { //Capitalized name for classes are used in Java
private final init[] locations; //key final mean that, is must be assigned before object is constructed and can not be changed later.
public Test(int[] locations) {
this.locations = locations;//To access to class member, when method argument has the same name use `this` key word.
}
public boolean checkYourSelf(int value) { //This method is accessed only from a object.
for(int location : locations) {
if(location == value) {
return true; //When you use key word return insied of loop you exit from it. In this case you exit also from whole method.
}
}
return false; //Method should be simple and perform one task. So you can get more flexibility.
}
public static int[] locations = {1,2,3};//This is static array that is not part of object, but can be used in it.
public static void main(String[] args) { //This is declaration of public method that is not part of create object. It can be accessed from every place.
Test test = new Test(Test.locations); //We declare variable test, and create new instance (object) of class Test.
String result;
if(test.checkYourSelf(2)) {//We moved outside the string
result = "Hurray";
} else {
result = "Try again"
}
System.out.println(result); //We have only one place where write is done. Easy to change in future.
}
}
回答by user1071777
Declare
宣布
public static int[] locations={1,2,3};
outside of the main method.
在主要方法之外。
回答by BlackBox
Remove the public
keyword from int[] locations={1,2,3};
. An access modifier isn't allowed inside a method, as its accessbility is defined by its method scope.
从 中删除public
关键字int[] locations={1,2,3};
。方法内部不允许使用访问修饰符,因为其可访问性由其方法范围定义。
If your goal is to use this reference in many a method, you might want to move the declaration outside the method.
如果您的目标是在许多方法中使用此引用,您可能希望将声明移到方法之外。
回答by Sam
public static int [] locations={1,2,3};
public static test dot=new test();
Declare the above variables above the main method and the code compiles fine.
在 main 方法上方声明上述变量,代码编译正常。
public static void main(String[] args){