Compute the Maximum of Some Specified Integers Using a Varargs Method - Java Object Oriented Design

Java examples for Object Oriented Design:Method Parameter

Description

Compute the Maximum of Some Specified Integers Using a Varargs Method

Demo Code

public class Main {
  public static int max(int n1, int n2, int... num) {    
    // Initialize max to the maximu of n1 and n2
    int max = (n1 > n2 ? n1 : n2);
    //  w  w  w  .  java 2 s  .  co  m
    for(int i = 0; i < num.length; i++) {
      if (num[i] > max) {
        max = num[i];
      }
    }
    return max;
  }
  
  public static void main(String[] args) {        
    System.out.println("max(7, 9) = " + Main.max(7, 9));
    System.out.println("max(7, 1, 3) = " + Main.max(7, 1, 3));
    System.out.println("max(-7, -1, 3) = " + Main.max(-7, -1, 3));
  }
}

Result


Related Tutorials