打印输入数组的平均值

时间:2015-05-26 20:43:17

标签: java

我有以下代码用于计算数组中的平均值:

package javaapplication2;

import java.util.Scanner;


public class JavaApplication {

public static void main(String[] args) {
    Scanner user_input = new Scanner(System.in);

    int valuen;
    System.out.println("enter the amount of numbers");
    valuen = user_input.nextInt();

    while (valuen<=1){
    System.out.println("Please enter a value greater than 1");
    valuen = user_input.nextInt();
    }
    //create an array
    double[] numbers = new double[valuen];
    int i; 
    double sum = 0;

    for (i=0; i < numbers.length ; i++){
        System.out.println("Please enter a number");
        numbers[i] = user_input.nextDouble();
        sum = sum + numbers[i];

    }
     double average = sum / numbers.length;
     System.out.println("the average is" + average);


}

输入数字后,代码刚刚结束,有人可以帮我显示平均值吗?

0 个答案:

没有答案