简体   繁体   中英

output blank--Java program to calculate average of array

I am writing a program that takes 10 floating point numbers as inputs, and displays the average of the numbers followed by all of the numbers that are greater than the average. I am using a method that takes an array of doubles as a parameter and returns the average of the data in the array. However, my problem is that when I run my program the output window is completely blank. I assume this is because I did not call in my method to the main method. However, I am not sure how to write that code. Thank you.

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;





}

}

to answer the main question...

However, my problem is that when I run my program the output window is completely blank. I assume this is because I did not call in my method to the main method. However, I am not sure how to write that code.

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

Or maybe you are thinking something like this...

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

A output run from above (with the doubles given):

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 . . .

If you have question about the code itself, then it would be better to create a new question or edit it to make it more clear.

Good luck with your coding.

Your method average() takes an array of doubles, but then retrieves an other array from standard input. That doesn't make sense.

Either get the doubles and pass them to the method, or don't pass them to the method and get them from standard input.

What you need to do is create an instance of Average class in your main method and call the average() method.

Why parse double array in your average() method when you take the input from user?

well,you are using a non static method "average()" for your task which needs an instances of the class to run which are not creating anywhere.so there are only two options:-

#1.create an instances of your class then call it.
   public static void main(String... s)
   {
        Average obj=new Average();
         obj.average();
   }


#2.make "average()" a static method by adding static keyword.

   public static double average()
   {

     //your code.....
   }

   public sttatic void main(String... s)
   {

      average();
   }
your dont need to keep an argument in your method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM