2013-12-19 9 views
-2

저는 완전히 잘못된 atm을하고 있습니다. im는 코딩에 익숙하지 않습니다. 그냥 시도.오버로드하는 방법에 문제가 있습니다.

숫자 배열의 평균을 반환하는 두 가지 오버로드 메서드를 만듭니다. 하나의 방법은

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}은 매개 변수로 double을 허용하는 평균 메소드를 테스트합니다.

지금 내 코드는 아마도 끔찍한 모양 일 것입니다. 나는 정말로 모른다.

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

숙제가 없습니다. 어떤 사람들은 책을 읽고, 어떤 사람들은 인터넷을 읽고 ... 이것을 얻고, 어떤 사람들은 실제로 질문을합니다. 아무도 그것을 파란에서 알지 못합니다. 그가 자신의 문제를 배우지 않고 복사하여 붙여 넣기를 원한다면 그것은 여전히 ​​유효한 질문이며 그 질문에 대한 유효한 대답입니다. –

관련 문제