Source of FindMax.java


  2: import java.util.Scanner;

  4: /**
  5:  * A program to find the maximum of numbers entered by the user.
  6:  *
  7:  * @author Mark Young (A00000000)
  8:  */
  9: public class FindMax {

 11:     public static void main(String[] args) {
 12:         // Declare and initialize variables
 13:         Scanner kbd = new Scanner(System.in);
 14:         int max = Integer.MIN_VALUE;
 15:         int n;

 17:         // Identify myself...
 18:         System.out.print("\n\n\n\n\n\n\n\n\n\n"
 19:                 + "Find Maximum\n"
 20:                 + "============\n\n"
 21:                 + "Finds the maximum value of positive integers "
 22:                 + "entered by the user.\n"
 23:                 + "Enter a negative number to stop input.\n\n\n\n");

 25:         // Get the first number from the user
 26:         System.out.print("Enter a number: ");
 27:         n = kbd.nextInt();

 29:         // keep going until the user entered a negative number
 30:         while (n >= 0) {
 31:             // update the maximum
 32:             max = Math.max(max, n);

 34:             // get the next number
 35:             System.out.print("Enter another number or -1 to quit: ");
 36:             n = kbd.nextInt();
 37:         }

 39:         // Print the result
 40:         System.out.print("\n\nMaximum = "
 41:                 + max
 42:                 + ".\n\n");
 43:     }

 45: }