【问题标题】:How to fix 'else without if' in Netbeans [duplicate]如何在Netbeans中修复'else without if' [重复]
【发布时间】:2019-01-13 11:14:08
【问题描述】:

我正在尝试在 Netbeans 中运行这个简单的代码,并以某种方式不断收到“else without if”错误。

我也在 E​​clips 中运行了这个,我得到了 else 应该转换为 while 的评论,这让我更加困惑。 我已经做了一些这样的例子,它们奏效了。

我还省略了 if 语句末尾的分号,但它不起作用。

public static void main(String[] args) {

    int a = 4;
    int b = 5;
    boolean negative = false;

    if (negative && (a < 0 && b < 0)); 
    {  
    System.out.println("true");
    }
    else((a < 0 && b > 0) || (a > 0 && b < 0));
    {
    System.out.println("false");
    }

【问题讨论】:

  • if (negative &amp;&amp; (a &lt; 0 &amp;&amp; b &lt; 0)); if 和else if 语句中去掉尾随的;
  • 第二个问题是else不带任何条件。如果你想添加一个,你需要另一个if(condition),比如if (isBlueTest) {handle blue} else if(isRedTest) {handle red} else {handle any other cases}

标签: java


【解决方案1】:

问题是你给 else 语句加上了一个条件。您只能在 else if 语句上设置条件

public static void main(String[] args) {

    int a = 4;
    int b = 5;
    boolean negative = false;

    if (negative && (a < 0 && b < 0)) {  
        System.out.println("true");
    } else if((a < 0 && b > 0) || (a > 0 && b < 0)){
        System.out.println("false");
    } else
        .....
}

【讨论】:

  • 不要说尝试,而是解释问题和给出的解决方案
  • 还是不行,'else'旁边的错误说:not a statement。
猜你喜欢
  • 1970-01-01
  • 2017-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-08
  • 1970-01-01
  • 2021-11-25
  • 1970-01-01
相关资源
最近更新 更多