Monday 6 June 2016

Chapter 3 Exercise 8, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

*3.8 (Sort three integers) Write a program that prompts the user to enter three integers and display the integers in non-decreasing order.


import java.util.Scanner;
 
public class ProgrammingEx3_8 {
 
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
 
  System.out.print("Enter three integers:");
  int number1 = input.nextInt();
  int number2 = input.nextInt();
  int number3 = input.nextInt();
  int temp = 0;
 
  if (number1 > number2) {
   temp = number1;
   number1 = number2;
   number2 = temp;
  }
 
  if (number2 > number3) {
   temp = number2;
   number2 = number3;
   number3 = temp;
  }
 
  if (number1 > number2) {
   temp = number1;
   number1 = number2;
   number2 = temp;
  }
 
  System.out.println(number1);
  System.out.println(number2);
  System.out.println(number3);
 
 }
 
}

No comments :

Post a Comment