Java函数


Java函数

public class DemoJava {

   public static void main(String []args) {

      int[] set1 = {10, 20, 30, 40, 50};
      int[] set2 = {101, 201, 301, 401, 501};
      int max;

      /* Process first set of numbers available in set1[] */
      max = getMax(set1);
      System.out.format("Max in first set = %d\n", max );

      /* Now process second set of numbers available in set2[] */
      max = getMax(set2);
      System.out.format("Max in second set = %d\n", max );
   }

   public static int getMax( int set[] ) {

      int i, max;

      max = set[0];
      i = 1;    
      while( i < 5 ) {

         if( max <  set[i] ) {
            max = set[i];
         }
         i = i + 1;
      }

      return max;
   }
}