【问题标题】:Java Error: illegal start of expressionJava 错误:表达式的非法开始
【发布时间】: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


【解决方案1】:

方法只能声明局部变量。这就是为什么当您尝试将其声明为公共时编译器会报告错误的原因。

对于局部变量,您不能使用任何类型的访问器(公共的、受保护的或私有的)。

您还应该知道 static 关键字的含义。在方法checkYourself 中,您使用整数数组locations

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

【讨论】:

  • 我的编译器没有抛出错误(我只是在 main 之外声明了 public static int[] 位置)。你确定方法只能声明局部变量吗?
  • 是的。当您将声明移到 main 之外时,它不再在方法的范围内,而是在类的范围内。然后你就可以公开它了。
  • @AnujaPL,我已经重写了您设计的课程,并提供了一些将来可能会有所帮助的解释。
【解决方案2】:

int[] locations={1,2,3}; 中删除public 关键字。方法内部不允许使用访问修饰符,因为它的可访问性由其方法范围定义。

如果您的目标是在多个方法中使用此引用,您可能希望将声明移到方法之外。

【讨论】:

    【解决方案3】:
    public static int [] locations={1,2,3};
    
    public static test dot=new test();
    

    在main方法上方声明上述变量,代码编译正常。

    public static void main(String[] args){
    

    【讨论】:

      【解决方案4】:

      声明

      public static int[] locations={1,2,3};
      

      在 main 方法之外。

      【讨论】:

      • 那行得通。你能告诉我为什么我需要这样做吗? public 关键字本身不会确保我在 main 内外的所有类和方法都可以访问它吗?
      • 你不能在方法中使用 public。如果您希望其他方法/类能够访问locations,则需要在您的类中将其声明为public。
      • 您不应将公共静态分配用于数据流概念。它解决了这个问题,但有点造成设计问题。
      猜你喜欢
      • 1970-01-01
      • 2015-05-30
      • 2014-03-09
      • 1970-01-01
      • 1970-01-01
      • 2017-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多