Compare2.java.html

  1  import java.util.Scanner;
  2  
  3  /**
  4     This program demonstrates comparisons of numbers, using Boolean expressions.
  5  */
  6  public class Compare2
  7  {
  8     public static void main(String[] args)
  9     {  
 10        Scanner in = new Scanner(System.in);
 11  
 12        System.out.println("Enter two numbers (such as 3.5 4.5): ");
 13        double x = in.nextDouble();
 14        double y = in.nextDouble();
 15        if (x == y) 
 16        {
 17           System.out.println("They are the same.");
 18        }
 19        else 
 20        {
 21           System.out.print("The first number is ");
 22           if (x > y)
 23           {
 24              System.out.println("larger");
 25           }
 26           else
 27           {
 28              System.out.println("smaller");
 29           }
 30  
 31           if (-0.01 < x - y && x - y < 0.01)
 32           {
 33              System.out.println("The numbers are close together");
 34           }
 35  
 36           if (x == y + 1 || x == y - 1)
 37           {
 38              System.out.println("The numbers are one apart");
 39           }
 40  
 41           if (x > 0 && y > 0 || x < 0 && y < 0)
 42           {
 43              System.out.println("The numbers have the same sign");
 44           }
 45           else
 46           {
 47              System.out.println("The numbers have different signs");
 48           }
 49        }
 50     }
 51  }
 52  
 53