Java求出给定三个数中的最大值

一般做法 ,利用if...else语句实现

public class Three62 {

	public static void main(String[] args) {

		int i=120,j=119,k=110;
		int max;
		if(i>j)
			max=i;
		else
			max=j;
		if(max>k)
			System.out.println(max);
		else
			System.out.println(k);

	}

}

 这种方法利用了三目运算符表达式,瞬间提高了一个逼格,而且看起来简洁易懂

三目运算符:条件?结果1:结果2,满足条件得到结果1,不满足条件得到结果2

public class Three62 {

	public static void main(String[] args) {

		int i=120,j=119,k=110;
		int max;
		max = (i>j)?i:j;
		max = (max>k)?max:k;
		System.out.println(max);

        }

}

 

你可能感兴趣的:(Java)