用三元运算符比较两个整数是否相等以及取三个数中的最大值

1.比较两个整数是否相等:

class Hello2 {
    public static void main(String[] args) {
            int x = 10;
            int y = 5;
            boolean b = (x == y) ? true : false;
            System.out.println("b = " + b);

    }

}

结果:

true : false可以省略,因为(x = y)这个判断的结果不是true就是false.

2.取三个数中的最大值:

class Hello2 {
    public static void main(String[] args) {
            int a = 10;
            int b = 20;
            int c = 30;
            int temp = (a > b) ? a : b;
            int max = (temp > c) ? temp : c;
            System.out.println("max = " + max);

    }

}

结果:

原文地址:https://www.cnblogs.com/Wangzui1127/p/11146815.html