使用用户输入获取均值(JAVA)

时间:2015-04-08 04:14:16

标签: java methods

我正在尝试获取用户输入,并使用他们的输入来计算平均值。我遇到的问题是我的代码不会提示用户使用整数来计算平均值。

这只是代码片段。

public static double[] getUserInput() {

    Scanner sc = new Scanner(System.in);

    List<Double> inputList = new ArrayList<Double>();

    System.out.println("Please enter a number");


    System.out.println(inputList);
    double arr[] = new double[inputList.size()];
    System.out.println(inputList.size());
    return arr;
}

public static double arithmeticMean(double[] nums) {

    double mean = 0;
    double sum = 0;

    // gets the mean
    try {
        for (int i = 0; i < nums.length; i++) {
            sum = sum + nums[i];
        }
        mean = sum / nums.length;
    } catch (ArithmeticException ex) {
        System.out.println(ex);
    }

    return mean;
}

2 个答案:

答案 0 :(得分:3)

问题是你永远不会阅读输入。实现扫描程序和读取用户输入的正确方法如下:

Scanner sc = new Scanner(System.in);

double userInput = 0;

System.out.print("Please enter a number");

userInput = sc.nextDouble();    // This is what you are missing

那么你可以将变量userInput添加到ArrayList中,或者直接读入ArrayList。

<强>更新

这是您想要的代码。它会询问用户输入的数量,然后将每个输入添加到数组中。

public static double[] getUserInput() {

    Scanner sc = new Scanner(System.in);

    List<Double> inputList = new ArrayList<Double>();

    System.out.println("Please enter how many numbers you will be inputing");
    int numberOfInputs = sc.nextInt();

    for (int i = 0; i < numberOfInputs; i++) {
        System.out.println("Please enter a number");
        double userInput = sc.nextDouble(); // Store user inputed double into temporary variable
        inputList.add(userInput); // Add temporary variable into ArrayList
    }
    sc.close();

    double[] arr = new double[inputList.size()];
    for (int i = 0; i < arr.length; i++) {
        arr[i] = inputList.get(i);
    }
    return arr;
}

答案 1 :(得分:1)

我猜你是不是想要这样做:

public static void main(String[] args) {
    Double[] arr = getUserInput();
    System.out.println("The Arithmetic Mean is " + arithmeticMean(arr));
}

public static Double[] getUserInput() {
    List<Double> inputList = new ArrayList<Double>();
    Scanner sc = new Scanner(System.in);

    System.out.println("Please one number at a time, or [Enter] to end.");
    while (true) {
        System.out.print("Next number: ");
        String input = sc.nextLine();
        if (input.equals(""))
            break;
        try {
            inputList.add(Double.parseDouble(input));
        }
        catch (NumberFormatException e) {
            System.out.println("Please enter a valid number, or [Enter] to end.");
        }
    }
    sc.close();

    Double[] arr = new Double[inputList.size()];
    arr = inputList.toArray(arr);
    return arr;
}

public static double arithmeticMean(Double[] nums) {
    double mean = 0;
    double sum = 0;

    // gets the mean
    try {
        for (int i = 0; i < nums.length; i++) {
            sum = sum + nums[i];
        }
        mean = sum / nums.length;
    } catch (ArithmeticException ex) {
        System.out.println(ex);
    }
    return mean;
}