previous | start | next

If Statements: Example 1

    1   
    2   import java.util.Scanner;
    3   
    4   /**
    5    * Description: Finds the maximum of two input integers.
    6    * 
    7    * @author glancast
    8    * 
    9    */
   10   public class Max2 {
   11   
   12     public static void main(String[] args) {
   13       int n, m;
   14       int max;
   15       Scanner in = new Scanner(System.in);
   16   
   17       System.out.println("This program computes the maximum of two input integers");
   18       System.out.print("\nFirst integer: ");
   19       n = in.nextInt();
   20       System.out.print("\nSecond integer: ");
   21       m = in.nextInt();
   22   
   23    if (n < m) {
   24         max = m;
   25       } else {
   26         max = n;
   27       }
   28       System.out.printf("The larger of %d and %d is %d\n", n, m, max);
   29   
   30     }
   31   
   32   }


previous | start | next