/** 最经典的C语言算法!*/public class 第十五题三个数排序 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("请输入三个整数: "); int a = in.nextInt(); int b = in.nextInt(); int c = in.nextInt(); int temp = 0; //首先如果 a > b,那么把a和b的值互换,变成了 a < b if(a > b) { temp = a; a = b; b = temp; } //其次如果a > c,那么把a和c的值互换,变成了a < c if(a > c) { temp = a; a = c; c = temp; } //最后如果b > c,那么把b和c的值互换,变成了b < c,此时就能确定a < b < c,排序完成 if(b > c) { temp = b; b = c; c = temp; } System.out.println("按照升序排序:" + a + " " + b + " " + c); // System.out.println("按照降序排序:" + c + " " + b + " " + a); in.close(); } }
原文地址:https://www.cnblogs.com/zjulanjian/p/10952669.html
时间: 2024-10-12 15:47:57