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;

^

Can any one explain why this error happens here?

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.

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

解决方案

i = 900;

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

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

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.

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值