JAVA 7.8

  1. import java.util.Scanner;
  2. public class Sumarray {
  3.     public static void main(String[] args) {
  4.     int[] iBucket = new int[10];
  5.     double [] dBucket = new double[10];
  6.     Scanner input =  new Scanner(System.in);
  7.     int i;
  8.     for ( i=0 ; i<10 ; i++ )
  9.         {
  10.         iBucket[i] = input.nextInt();
  11.         dBucket[i] = input.nextDouble();
  12.         }
  13.     System.out.println(sum (iBucket));
  14.     System.out.println(sum (dBucket));
  15.     }
  16.     public static int sum (int[] array )
  17.     {
  18.     int j,iSum;
  19.     iSum = 0;
  20.     for ( j=0 ; j<10 ; j++ )
  21.         iSum += array[j];
  22.     return iSum;
  23.     }
  24.     public static double sum (double[] array )
  25.     {
  26.     int j;
  27.     double dSum = 0.0;
  28.     for ( j=0 ; j<10 ; j++ )   
  29.         dSum += array[j];
  30.     return dSum;   
  31.     }
  32. }

Overloading Methods
overloading methods enables you to define the methods with the same name as long as their signatures are different.


留言

這個網誌中的熱門文章

Codeforces --- string task

Uva 674 ---- coin change

codeforces 271A --- beautiful year