Java min&max

By | 11/11/2014

The program asks for three numbers and prints either the biggest or the smallest of them depending which one you choose.

BiggestOrSmallestNumber.java:

import java.util.Scanner;

public class BiggestOrSmallestNumber {
 public void run()
 {
  Scanner numberReader=new Scanner(System.in);
  Scanner textReader = new Scanner(System.in);
  int number1, number2, number3;
  String biggestSmallest;
  System.out.println("Enter three numbers:");
  number1=numberReader.nextInt();
  number2=numberReader.nextInt();
  number3=numberReader.nextInt();
		
  System.out.print("Do you want to know which was the biggest or smallest? ");
  biggestSmallest=textReader.nextLine();
		
  numberReader.close();
  textReader.close();
		
  //Check if biggest or smallest
  if(biggestSmallest.equals("biggest")||biggestSmallest.equals("Biggest")) {
   System.out.println("The biggest number was: "+Math.max(number1, Math.max(number2, number3)));
  }
  else if(biggestSmallest.equals("smallest")||biggestSmallest.equals("Smallest")) {
   System.out.println("The smallest number was: "+Math.min(number1, Math.min(number2, number3)));
  }
  else {
   System.out.println("Something went wrong");
  }
 }
 public static void main(String[] args)
 {
  BiggestOrSmallestNumber program = new BiggestOrSmallestNumber();
  program.run();
 }
}

Leave a Reply

Your email address will not be published. Required fields are marked *