Java if-if-else behavior

后端 未结 7 687
夕颜
夕颜 2020-12-11 21:18

I wrote a simple if/else in my code which worked fine. Later I added another level of if under the first, and was baffled by its behavior. Here\'

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-11 21:29

    Is there a way to make the above code do what the indentation makes you expect without adding braces?

    I would go:

    public static void main(String[] args) {
        boolean a = true;
        boolean b = false;
    
        if (!a)
            System.out.println("a=false");   
        else if(b)
            System.out.println("a=true, b=true");
    
    }
    

提交回复
热议问题