Java错误:表达式的非法开始 [英] Java Error: illegal start of expression

查看:143
本文介绍了Java错误:表达式的非法开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上是提炼,完成和试图编译测试代码从一个参考书为java初学者。目标是创建一个猜测游戏,其中目标位于3个连续单元格(我正在控制数组中的位置),用户猜测单元格。以细胞破坏目标细胞。



我在同一个错误上检查了六个帖子,但我无法弄清楚出了什么问题。 p>

这是我的错误:

  test.java:5:非法开始表达式
public int [] locations = {1,2,3};
^
1错误

,我的代码是:

  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;
}

}


解决方案

方法只能声明局部变量。那为什么编译器报告错误,当你试图声明它是公共的。



如果是局部变量,则不能使用任何类型的访问器(public,protected或private)。



你还应该跟踪静态关键词的含义。在方法 checkYourself 中,您使用 locations 的声明。



静态关键字区分可以通过对象创建访问的元素。有没有对象本身的一部分。

  public class Test {//类别的大小写名称用于Java 
private final ini [] locations; // key final的意思是,在对象被构造之前必须被赋值,以后不能被改变。

public Test(int [] locations){
this.locations = locations; //访问类成员,当方法参数具有相同的名称时使用`this`关键字。
}

public boolean ckeckYourSelf(int value){//此方法仅从对象访问。
for(int location:locations){
if(location == value){
return true; //当你使用关键字返回的时候,你退出循环。在这种情况下,你也从整个方法退出。
}
}
return false; //方法应该简单,并执行一个任务。所以你可以更灵活。
}
public static int [] locations = {1,2,3}; //这是不是对象的一部分但是可以在其中使用的静态数组。

public static void main(String [] args){//这是不是create对象的公共方法的声明。它可以从每个地方访问。
Test test = new Test(Test.locations); //我们声明变量test,并创建Test类的新实例(obect)。
String result;
if(test.checkYourSelf(2)){//我们移出了字符串
result =Hurray;
} else {
result =Try agian
}
System.out.println(result); //我们只有一个地方写入。容易改变未来。
}
}


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.

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;
        }

}

解决方案

Methods can declare only local variables. That why compiler report an error when you try to declare it as public.

In case of local variables you can not use any kind of accessor (public, protected or private).

You should also keep track on what static key word means. In method checkYourself, you use the declaration of locations.

The static key word distinct the elements that are accessible with object creation. There for there are not part of object itself.

public class Test { //Capitalized name for classes are used in Java
   private final ini[] 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 ckeckYourSelf(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 ge 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 (obect) of class Test.  
      String result;
      if(test.checkYourSelf(2)) {//We moved outsie the string
        result = "Hurray";        
      } else {
        result = "Try agian"
      }
      System.out.println(result); //We have only one place where write is done. Easy to change in future.
   } 
}

这篇关于Java错误:表达式的非法开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆