Java input three integers and output them from small to large of x y z

  • 2020-06-15 08:33:28
  • OfStack

Enter 3 integers x,y,z. Please output these 3 integers from small to large.

Program analysis: We try to put the smallest number on x and compare x to y, if x > y swaps the values of x with y, and then compares x with z, if x > z swaps the values of x and z to minimize x.

Program design:


import java.util.*;
public class test {
  public static void main (String[]args){
    int i=0;
    int j=0;
    int k=0;
    int x=0;
    System.out.print(" Please enter the 3 The number of \n"); 
    Scanner input = new Scanner(System.in);
    i=input.nextInt();
    j=input.nextInt();
    k=input.nextInt();
    if(i>j)
    {
     x=i;
     i=j;
     j=x;
    }
    if(i>k)
    {
     x=i;
     i=k;
     k=x;
    }
    if(j>k)
    {
     x=j;
     j=k;
     k=x;
    }
    System.out.println(i+", "+j+", "+k);
  }
}


Related articles: