Java中的三元(三目)运算符

public class Demo { // 使用if...else
	public static void main(String[] args) {
		int i =10;
		int j = 20;
		int max = getMaxNum(i,j);
		System.out.println("Max:" + max);
		
	}
	public static int getMaxNum(int a,int b) {
		if (a > b) {
			return a;
		}else {
			return b;
		}
	}
}

// 使用三元运算符【如果i>j为真,则表达式取i值,否则取j值.】

public class Demo {
	public static void main(String[] args) {
		int i =10;
		int j = 20;
		int max = i > j ? i : j; // 三元运算符
		System.out.println("Max:" + max);
	}
}

eclipse执行结果:
	均为 Max:20
posted @ 2019-08-02 12:28  每一天,为明天。  阅读(190)  评论(0)    收藏  举报