简体   繁体   English

错误:Java中应有标识符

[英]Error: identifier expected in Java

I am a new learner of Java. 我是Java的新手。 I learned some of the Java core concepts. 我学习了一些Java核心概念。 I got the identifier expected error when run my following code: 运行以下代码时,出现标识符预期错误:

class Sekar {
  public static int i,j,k;
  i = 900;
  static void max()
  {
    j = 100;
    if(i>j)
    {
      k=i;
    }
    else {
      k=j;
    }
    System.out.println("The maxmimum vale between"+i+"and"+j+"is :"+k);
  }               
  public static void main(String[] args) {
     max();       
  }
}

When I compile my code, I get the following error: 编译代码时,出现以下错误:

error: identifier expected
  i    =  900;
     ^
  1. Can any one explain why this error happens here? 谁能解释为什么此错误在这里发生?
  2. When I google about identifier expected error, I found that this error happens when variables are declared without datatype, but I declared that for all my variables i,j,k. 当我用谷歌搜索标识符预期错误时,我发现当声明变量而没有数据类型时会发生此错误,但是我对所有变量i,j,k都声明了这一错误。
  3. When I redeclare the data type again while setting value to "i" like int i = 900 it works. 当我像int i = 900这样将值设置为“ i”时再次声明数据类型时,它可以工作。 Why does it? 为什么呢
i = 900;

This is a statement in Java, it can be inside Constructor or method, or initialization block. 这是Java中的一条语句,它可以在Constructor或方法内,也可以在初始化块内。

In your case, you may move that inside the max() method 就您而言,您可以将其移动到max()方法中

When I re declare the data type again while setting value to "i" like int i = 900 it works. 当我将值设置为“ i”(如int i = 900时再次声明数据类型时,它将起作用。 Why does it? 为什么呢

Here, you are declaring and assigning the value to the variable in the same time, same line. 在这里,您要在同一时间,同一行中声明并分配该值。

Check here for more details and here about java statements, expressions 在此处查看更多详细信息,并在此处查看有关Java语句,表达式的信息

Statements 声明

Statements are roughly equivalent to sentences in natural languages. 陈述大致相当于自然语言中的句子。 A statement forms a complete unit of execution. 一条语句构成了完整的执行单元。 The following types of expressions can be made into a statement by terminating the expression with a semicolon (;). 通过使用分号(;)终止表达式,可以将以下类型的表达式制成语句。

  • Assignment expressions 赋值表达式
  • Any use of ++ or -- 任何使用++或-
  • Method invocations 方法调用
  • Object creation expressions 对象创建表达式

Hava a look at Java: Identifier expected : Hava看看Java:期望的标识符

i = 900;

is a statement as any other. 和其他声明一样。 You can't write statement anywhere. 您无法在任何地方编写语句。 It must be in methods/constructors body. 它必须在方法/构造函数主体中。 Initializing variable in declaration is called definition and is exception to this rule. 声明中的初始化变量称为定义 ,是该规则的例外。

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/expressions.html http://docs.oracle.com/javase/tutorial/java/nutsandbolts/expressions.html

If you want to initialize static variable you can do it 2 (sane) ways: Initialize variable right where you are declaring it: 如果要初始化静态变量,可以使用2种(合理的)方法:在声明它的位置初始化变量:

class Sekar {
  public static int i = 900, j,k;

  static void max()
  {
    j = 100;
    if(i>j)
    {
      k=i;
    }
    else {
      k=j;
    }
    System.out.println("The maxmimum vale between"+i+"and"+j+"is :"+k);
  }               
  public static void main(String[] args) {
     max();       
  }
}

or do it in static constructor: 或在静态构造函数中执行:

class Sekar {
  public static int i, j,k;

  static {
    i = 900;
  }

  static void max()
  {
    j = 100;
    if(i>j)
    {
      k=i;
    }
    else {
      k=j;
    }
    System.out.println("The maxmimum vale between"+i+"and"+j+"is :"+k);
  }               
  public static void main(String[] args) {
     max();       
  }
}

Also, if you want to define a constant I recommend using final keyword. 另外,如果要定义常量 ,建议使用final关键字。

j could be converted to local variable . j可以转换为局部变量

class Sekar {
  public static final int I = 900;

  static void max()
  {
    int k;
    int j = 100;
    if(I>j)
    {
      k=I;
    }
    else {
      k=j;
    }
    System.out.println("The maxmimum vale between"+I+"and"+j+"is :"+k);
  }               
  public static void main(String[] args) {
     max();       
  }
}

What you probably want to do is this: 您可能想做的是:

class Sekar {
    public static int i=900,j=100,k;
    static void max()
    {
        if(i>j)
        {
            k=i;
        }
        else {
            k=j;
        }
        System.out.println("The maxmimum vale between"+i+"and"+j+"is :"+k);
    }
    public static void main(String[] args) {
        max();
    }
}    

However I would discourage you from using static fields in this case. 但是,在这种情况下,我不鼓励您使用静态字段。 I would suggest you to make i , j and k parameters to your method. 我建议您为您的方法设置ijk参数。 And give them descriptive names while you're at it. 并在使用时为他们提供描述性名称。

Also note that k is not initialised explicitly and is therefore set to 0 by default, so your else clause is never reached. 还要注意, k没有显式初始化,因此默认情况下设置为0 ,因此永远不会到达else子句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM