2013-12-19 110 views
-2

我知道我做这个完全错误的atm,我是新编码,我知道我做什么都是错的。尽管如此。重载方法问题

创建两个重载方法,它们返回数组数组的平均值。一种方法是

public static double average (int[] array) 

其他方法的标题是

public static double average (double[] array) 

我我想要调用这两种,并打印平均主要方法。第一个数组使用{1,2,3,4,5}来测试接受整数的平均方法,并使用这个数组{6.0,5.0,4.0,3.0,2.0,1.0}来测试接受双精度的平均方法作为参数。

现在我的代码,可能看起来很干净。我真的不知道。

public class methodss 
{ 
    public static void main(String[] args) 
    { 
     //invoke the average int method 
     System.out.println("Average with int: " + average); 
     //invoke the average double method 
     System.out.println("Average with double: " + average); 

     public static double average (int[] array) 
     { 
      int sum = 0, average = 0; 

      array[5] = {1,2,3,4,5} 
      for (int i = 0; i < 10; ++i){ 
       sum+=array[i]; 
       average = sum/5; 
       return average; 
      } 
     } 

     public static double average (double[] array) 
     { 
      int sum = 0, average2 = 0; 

      array[6] = {6.0, 5.0, 4.0, 3.0, 2.0, 1.0}; 
      for (int x = 0; x < 10; ++x){ 
       sum+=array[x]; 
       average = sum/6; 
       return average; 
     } 
    } 
} 
+0

你可以做methodss.average(新INT [0])和methodss.average(新双[0]); –

+1

如果你知道你完全错了,那么停下来学习正确的方法。 –

回答

2

试试这个,

// Classes normally are named like proper nouns, so they start with a captial letter 
public class Methods 
{ 
    // main is the entry point of the app. 
    // all the args that are entered on the command line are passed in 
    // as an array of strings. 
    // the keyword static here means that this method belongs to this class 
    // so all invokations are in terms of the class. 
    // i.e. java Methods ... implicitly invokes Methods.main() 
    public static void main(String[] args) 
    { 
     //invoke the average int method 
      System.out.println("Average with int: " + Methods.average(1, 2, 3, 4, 5)); 
     //invoke the average double method 
      System.out.println("Average with double: " + Methods.average(1.0, 5.4, 2.3, 1.6); 
    } 

     /** 
     * takes the average of an arbitrary amount of primitive ints 
     * @param array a varg of ints, so that the caller can specify calls 
     * to the method like average(1) or average(1, 2) or average(new int[10]), 
     * makes calling this function in adhoc ways readable. 
     * @returns a double representing the average of all the elements in the arguments 
     **/ 
     public static double average (int... array) 
     { 
      int sum = 0; 
      double average = 0.0; 
      // loop through the array and stop when the iterator int, i 
      // reaches the amount of elements in the array. 
      // the amount of elements in the array can be read by the 
      // special instance variable on array types called length. 
      for (int i = 0; i < array.length; ++i){ 
       sum += array[i]; 
      } 
      // so that these don't get truncated, inserting a 1.0 to make sure it's 
      // a double. Otherwise the average of (1, 2) would be 1 instead of 1.5 
      average = 1.0 * sum/array.length; 
      return average; 
     } 


     /** 
     * Same as average(int...) but with doubles 
     **/ 
     public static double average (double... array) 
     { 

      double sum = 0.0, average = 0.0; 

      for (int x = 0; x < array.length; ++x){ 
       sum += array[x]; 
      } 
      average = sum/array.length; 
      return average; 
     } 
} 
+0

它看起来像家庭作业,所以我假设使用'double ... array'肯定会表明他没有自己做到这一点;) – bluewhile

+0

没有你自己的作业。一些人阅读书籍,一些人阅读互联网...并得到这个,有些人实际上提出问题。没有人知道它出于蓝色。如果他想复制和粘贴而不知道这是他的问题:)对于这个问题,这仍然是一个有效的问题和有效的答案。 –