输入三个整数,按由小到大的顺序输出

这个题学会写一些小函数

三个值求中间值的话,首先固定两个值的关系,考虑第三个变量和这两个固定关系的变量的关系。

 1 import java.util.Scanner;
 2 public class 三个整数排序 {
 3 
 4     public static int getMax(int x, int y) {
 5         return x>y?x:y;
 6     }
 7     public static int getMin(int x, int y) {
 8         return x<y?x:y;
 9     }
10     public static int GetMiddle(int x,int y,int z) {
11         int temp=0;
12         if(x>y)
13         {
14             temp=x;
15             x=y;
16             y=temp;
17         }
18         if(z<x) return x;
19         else if(z<y) return z;
20         else return y;        
21     }
22     public static void main(String[] args) {
23         Scanner scanner=new Scanner(System.in);
24         int x=scanner.nextInt();
25         int y=scanner.nextInt();
26         int z=scanner.nextInt();
27         System.out.println(getMin(x, getMin(z, y))+" "+GetMiddle(x, y, z)+" "+getMax(x, getMax(z, y))+" ");
28         
29     }
30 
31 }

猜你喜欢

转载自www.cnblogs.com/kongchung/p/9720817.html