¿Cómo puedo calcular la media aritmética en mi ejemplo de código?

I write a code sample about calculate the aritmetic mean,i did lots of thing but some parts are missing/incorrect.

Esta es mi muestra de código:

public static void main(String[] args) {
    int i;
    int j;
    int thelargest;
    int thesmallest;

    Scanner input = new Scanner(System.in);
    System.out.println("Enter the list of number : ");
    String input2 = input.nextLine();

    String[] numbers = input2.split("\\s+");

    int[] result = new int[numbers.length];
    for (j = 0; j < numbers.length; j++) {
        result[j] = Integer.parseInt(numbers[j]);

    }

    for (i = 0; i < result.length; i++) {
        System.out.print("");
        System.out.println(result[i]);
    }

    System.out.println("The Largest Number :  "
            + findTheLargestNumber(result));
    System.out.println("The Smallest Number :  "
            + findTheSmallestNumber(result));
    thelargest = findTheLargestNumber(result);
    thesmallest = findTheSmallestNumber(result);
    float arithmeticMean = (float) (result[i + j])// result.length;
            System.out.println("The Arithmetic Mean : " + arithmeticMean);

/*There is a mistake and I tried to solve it but I didn't find any way to solve it.I want my programme to sum the results(numbers) and divide into number of result.(For example :10+20+30=60 and the aritmetic mean is 60/3=20.)Lastly,I think the mistake is about (float)(result[i+j]).

*/

}

public static int findTheSmallestNumber(int[] series) {
    int thesmallest = series[0];
    for (int i = 1; i < series.length; i++) {
        if (series[i] < thesmallest) {

            thesmallest = series[i];
        }
    }
    return thesmallest;
}

public static int findTheLargestNumber(int[] series) {
    int thelargest = series[0];
    for (int i = 1; i < series.length; i++) {
        if (series[i] > thelargest) {

            thelargest = series[i];
        }
    }
    return thelargest;
}

}

Thanks for everbody which gonna help to me .

preguntado el 05 de mayo de 13 a las 14:05

result[i + j] this should throw an ArrayIndexOutOfBounds excepción. -

result[i + j] returns the element at the i+j-th position: (a) there might no item at this position and you will get an exception (b) even if there were an element at that position, there is no reason that it would be equal to the mean. You need to loop over the items and calculate the mean manually. -

@assylias but I want to calculate the mean automaticly with my programme .How can i change that ? -

@MarounMaroun Ok,but what am I gonna do to solve the problem ? -

3 Respuestas

public static float getAirthmeticMean(int[] result){
   int sum = 0;
   for (int i = 0; i < result.length; i++) {
       sum+=result[i]);
   }
   return (float)sum/result.length;
}

contestado el 05 de mayo de 13 a las 15:05

thank you @NoobUnChained but I want to ask one last question.Where should I write this in my code sample ? - user2348367

Write that method inside the class like findTheLargestNumber() etc and call it passing the result array!!! - TodoTambiénSeñor

@NoobUnChained I can't do that can you show on a code sample ? - user2348367

System.out.println("The Arithmetic Mean : " + getAirthmeticMean(result)); - TodoTambiénSeñor

ok i did it but now the eclipse warns me about two errors about "i"? for (i = 0; i < result.length; i++) { sum+=result[i]);(All of the "i" are shown like an error.) - user2348367

Your instinct is correct. The result[i+j] line doesn't make sense. How about another function?

public static float calculateMean(int[] series) {
  int sum = 0;
  for(int i = 0; i < series.length; i++) {
    sum = sum + series[i];
  }
  return ((float)sum) / series.length;
}

contestado el 05 de mayo de 13 a las 14:05

Just add all the numbers in the array together in a for loop, and then divide by the length of the array.

contestado el 05 de mayo de 13 a las 14:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.