其他分享
首页 > 其他分享> > 条件操作

条件操作

作者:互联网

三元操作符
boolean-expression ? expression1 : expression2

//条件操作
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int x = 1,y = 0,z = 0;
		if(x > 0) {
			y = 1;
		}else {
			y = -1;
		}
		//等价于
		z =	(x > 0) ? 1 : -1;
		
		System.out.println(y);
		System.out.println(z);
		
		int number = 4;
		System.out.println((number % 2 == 0) ? "num is even" : "num is odd");
	}

}

判断三个数是否为正序(递增)


	public static void main(String[] args) {
		// TODO Auto-generated method stub
		java.util.Scanner input = new java.util.Scanner(System.in);
		double x = input.nextDouble();
		double y = input.nextDouble();
		double z = input.nextDouble();
		
		System.out.println((x < y && y < z) ? "正序":"非正序");
		
		input.close();
	}

}

标签:double,条件,System,input,println,操作,nextDouble,out
来源: https://blog.csdn.net/m0_49153993/article/details/112796310