Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
258 views
in Technique[技术] by (71.8m points)

Java Error: illegal start of expression

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

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Methods can only declare local variables. That is why the compiler reports an error when you try to declare it as 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.

The static keyword distinct the elements that are accessible with object creation. Therefore they are not part of the object itself.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...