Source of Comparison.java


  1: // Fig. 2.15: Comparison.java
  2: // Compare integers using if statements, relational operators 
  3: // and equality operators.
  4: import java.util.Scanner; // program uses class Scanner
  5: 
  6: public class Comparison 
  7: {
  8:    // main method begins execution of Java application
  9:    public static void main( String args[] )
 10:    {
 11:       // create Scanner to obtain input from command window
 12:       Scanner input = new Scanner( System.in );
 13: 
 14:       int number1; // first number to compare
 15:       int number2; // second number to compare
 16: 
 17:       System.out.print( "Enter first integer: " ); // prompt 
 18:       number1 = input.nextInt(); // read first number from user 
 19: 
 20:       System.out.print( "Enter second integer: " ); // prompt 
 21:       number2 = input.nextInt(); // read second number from user 
 22:       
 23:       if ( number1 == number2 ) 
 24:          System.out.printf( "%d == %d\n", number1, number2 );
 25: 
 26:       if ( number1 != number2 )
 27:          System.out.printf( "%d != %d\n", number1, number2 );
 28: 
 29:       if ( number1 < number2 )
 30:          System.out.printf( "%d < %d\n", number1, number2 );
 31: 
 32:       if ( number1 > number2 )
 33:          System.out.printf( "%d > %d\n", number1, number2 );
 34: 
 35:       if ( number1 <= number2 )
 36:          System.out.printf( "%d <= %d\n", number1, number2 );
 37: 
 38:       if ( number1 >= number2 )
 39:          System.out.printf( "%d >= %d\n", number1, number2 );
 40: 
 41:    } // end method main
 42: 
 43: } // end class Comparison
 44: 
 45: /**************************************************************************
 46:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 47:  * Pearson Education, Inc. All Rights Reserved.                           *
 48:  *                                                                        *
 49:  * DISCLAIMER: The authors and publisher of this book have used their     *
 50:  * best efforts in preparing the book. These efforts include the          *
 51:  * development, research, and testing of the theories and programs        *
 52:  * to determine their effectiveness. The authors and publisher make       *
 53:  * no warranty of any kind, expressed or implied, with regard to these    *
 54:  * programs or to the documentation contained in these books. The authors *
 55:  * and publisher shall not be liable in any event for incidental or       *
 56:  * consequential damages in connection with, or arising out of, the       *
 57:  * furnishing, performance, or use of these programs.                     *
 58:  *************************************************************************/