错误:Java中预期的标识符 [英] Error: identifier expected in Java

查看:869
本文介绍了错误:Java中预期的标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的新学习者。我学到了一些Java核心概念。运行以下代码时,我得到了标识符预期错误:

I am a new learner of Java. I learned some of the Java core concepts. 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. 任何人都可以解释为什么会出现此错误?

  2. 当我谷歌关于标识符预期错误时,我发现在没有数据类型的情况下声明变量时会发生此错误,但我为所有变量声明了i,j,k。

  3. 当我将值设置为i时再次重新声明数据类型,如 int 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.
  3. When I redeclare the data type again while setting value to "i" like int i = 900 it works. Why does it?


推荐答案

i = 900;

这是Java中的一个语句,它可以在Constructor或方法或初始化块中。

This is a statement in Java, it can be inside Constructor or method, or initialization block.

在您的情况下,您可以在 max()方法内移动

In your case, you may move that inside the max() method


当我将值设置为i时再次声明数据类型,如
int i = 900 它有效。为什么会这样?

When I re declare the data type again while setting value to "i" like int i = 900 it works. Why does it?

在这里,您在相同的时间内声明并将值赋给变量。

Here, you are declaring and assigning the value to the variable in the same time, same line.

检查此处有关更多详细信息,请参阅此处有关java语句,表达式

Check here for more details and here about java statements, expressions


语句

语句大致相当于句子中的句子自然语言。
语句构成一个完整的执行单元。通过使用分号(;)终止表达式
,可以将以下类型的
表达式转换为语句。

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

这篇关于错误:Java中预期的标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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