嵌套if-else如何在没有花括号的情况下工作?

时间:2018-06-05 17:41:05

标签: java

public class Main {
    public static void main(String[] args) {
        int aNumber = 3;
        if (aNumber >= 0)
            if (aNumber == 0)
                System.out.println("first string");
        else 
            System.out.println("second string");
        System.out.println("third string");

    }
}

输出

second string
third string

我希望

third string

因为System.out.println("second string");的其他内容应该是if (aNumber == 0)。 我的问题是

  • 在撰写外if
  • 之前,是否需要编写所有内部else' else
  • 是否允许在外if

    else - if语句之间撰写语句
    if(condition) {
        if(anotherCondition) {
            someCode();
        }
        //execute code here
    } else {
        anotherCode();
    }
    

0 个答案:

没有答案
相关问题