Java50道经典编程题:(三十四)排序问题 ——条件判断结构

1.问题重述

题目:输入3个数a,b,c,按大小顺序输出。

2.解析

先选定a,b,c中哪个最小,哪个最大,然后比价大小,根据实际情况先确定最大数或最小数,之后依次交换即可。

3.解决问题

代码如下:

//题目:输入3个数a,b,c,按大小顺序输出。
public class demo {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请依次输入三个数:");
		int a = scanner.nextInt();
		int b = scanner.nextInt();
		int c = scanner.nextInt();
		if(a > b) {
			int temp;
			temp = a;
			a = b;
			b = temp;
		}
		if(a > c) {
			int temp;
			temp = a;
			a = c;
			c = temp;
		}
		if(b > c) {
			int temp;
			temp = b;
			b = c;
			c = temp;
		}
		System.out.println(a + "<" + b + "<" + c);
		scanner.close();
	}
}

你可能感兴趣的:(java50道经典编程题)