java - 输出空白——计算数组平均值的Java程序

标签 java arrays for-loop methods program-entry-point

我正在编写一个程序,它以 10 个 float 作为输入,并显示这些数字的平均值,然后显示所有大于平均值的数字。我正在使用一种方法,该方法采用 double 组作为参数并返回数组中数据的平均值。但是,我的问题是,当我运行程序时,输出窗口完全是空白的。我认为这是因为我没有在我的方法中调用 main 方法。但是,我不确定如何编写该代码。谢谢。

import java.util.Scanner;

public class Average {

public static void main(String[] args) {
}

public double average(double[] number) {
    Scanner scanner = new Scanner(System.in);

    int x = 0;
    double sum = 0;
    double[] numberList = new double[10]; //array to hold all numbers
    double[] largerList = new double[10]; //array to hold numbers greater than the average


    int numberIndex = 0;
    int largerIndex = 0;



    System.out.printf("Please enter 10 floating-point numberes.\nIf more than 10 values are entered, the numbers following 10 are ignored.\nIf less than 10 numbers are entered, the program will wait for you to enter 10.\n");

    for (int i = 0; i < 10; i++) {


        try { //try catch exception to catch decimal inputs as well as more /less than 10 integers
            x = scanner.nextInt();
            sum += numberList[x]; //add up all inputs to find sum
        } catch (Exception e) {
            System.out.println("Invalid input! Please reenter 10 integer values.");
            scanner = new Scanner(System.in);
            i = -1;
            numberIndex = 0;
            largerIndex = 0;
            numberList = new double[10];
            largerList = new double[10];
            continue;
        }
    }

    for (int i = 0; i < number.length; i++) {
        sum = sum + number[i];
        double average = sum / number.length;
        System.out.println("Average value of your input is: " + average);
        System.out.println();

        //return average;

        if (x > average) {
            largerList[largerIndex] = x; //add negative input to negativeList array
            largerIndex = largerIndex + 1;

        }
    }



    for (int i = 0; i < largerIndex; i++) {
        System.out.println(largerList[i]);
    }
    return 0;





}

}

最佳答案

回答主要问题...

但是,我的问题是,当我运行程序时,输出窗口完全是空白的。我认为这是因为我没有在我的方法中调用 main 方法。但是,我不确定如何编写该代码。

public static void main(String[] args) {
    new Average().average(new double[10]);
}

或者也许你正在想这样的事情......

public static void main(String[] args) {
    double[] numbers = {2,3,4,5,6,4,3,2,1,3};
    new Average().average(numbers);
}

从上面运行的输出(给定 double ):

Please enter 10 floating-point numberes.
If more than 10 values are entered, the numbers following 10 are ignored.
If less than 10 numbers are entered, the program will wait for you to enter 10.
2
3
3
4
1
2
3
4
5
1
Average value of your input is: 0.2

Average value of your input is: 0.5

Average value of your input is: 0.9

Average value of your input is: 1.4

Average value of your input is: 2.0

Average value of your input is: 2.4

Average value of your input is: 2.7

Average value of your input is: 2.9

Average value of your input is: 3.0

Average value of your input is: 3.3

1.0
1.0
1.0
Press any key to continue . . .

如果您对代码本身有疑问,那么最好创建一个新问题或对其进行编辑以使其更加清晰。

祝您编码顺利。

关于java - 输出空白——计算数组平均值的Java程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21034182/

相关文章:

java - 查找从哪里加载 java 类

java - 为什么我们应该在 File.toPath() (jdk7) 中使用临时变量

c - 为什么我的带有 scanf 的 for 循环没有被执行?

java - 为什么 Java 8 的 Nashorn 引擎在严格模式下调用 apply() 并直接传递参数对象时会抛出 java.lang.ClassCastException?

stack-trace - 如何在 Java 中获取当前堆栈跟踪?

java - 如何合并两个不同的字符串

sql - 配置单元-展开数组列并使用带有选择语句错误的LEFT联接或子查询

java - 如何在 Java 中比较两个二维数组?

Java 8 : Parallel FOR loop

javascript - 初学者 JavaScript - 重新启动我的 for 循环