Java如何检查哪个条件为真

时间:2016-08-09 02:52:01

标签: java

有没有让程序说出哪个条件是正确的?

public static void main(String[] args) throws IOException, InterruptedException {
  Boolean a = true;
  Boolean b = false;
  Boolean c = null;

  if (a == true || b == false || c == true) {
    System.out.println("Test");
  }
}

由于a是唯一正确的,因此有办法让它打印"a is the only condition that is correct"

1 个答案:

答案 0 :(得分:1)

if (a) {
    System.out.println("a is the condition that is correct");
}
//Edit by comment. Thanks
if (!b) {
    System.out.println("b is the condition that is correct");
}

if (c) {
    System.out.println("c is the condition that is correct");
}