【问题标题】:While statement has no body?while 语句没有正文?
【发布时间】:2015-06-01 23:57:48
【问题描述】:

我在一个过程中有许多语句,如下所示:

while( x + ++flips < 8 && board[x + flips][y] == opponent );

(标准“Oracle”)编译器返回错误:“虽然语句没有正文。”这是语言规范要求的,还是编译器特定的?是否有某种 hack 我可以用来让它工作还是我被困在重写它?

【问题讨论】:

标签: java loops syntax while-loop


【解决方案1】:

您发布的代码compiles fine when I try itJLS section 14.12 允许将空语句作为while 循环的主体:

WhileStatement:
  while ( Expression ) Statement

Statement:
  StatementWithoutTrailingSubstatement 
  ...

StatementWithoutTrailingSubstatement:
  Block 
  EmptyStatement
  ...

EmptyStatement:
  ;

很可能,您的实际代码有错字。

【讨论】:

    【解决方案2】:

    只需添加一个空主体。喜欢这个while( x + ++flips &lt; 8 &amp;&amp; board[x + flips][y] == opponent ){}

    【讨论】:

    • 分号作为正文应该已经足够了。 JLS 允许它,当我尝试它时它编译得很好。
    猜你喜欢
    • 2017-08-11
    • 2022-01-17
    • 2016-02-29
    • 2013-07-18
    • 2012-02-07
    • 2018-08-03
    • 2015-01-10
    • 1970-01-01
    • 2016-01-10
    相关资源
    最近更新 更多