I am trying to put numbers into an array using user input then find the average of those number, and also find which numbers are greater than the average. The numbers are going into an array but when I am trying to find the average I am not able to find the average and which numbers are greater than the average because not all of the variables are visible to the part trying to find the numbers greater than the average. But, when I allow that part to see all of the variables (don't but {} around some parts) it finds the average for each individual number. So right now it prints the average for each number, and it prints null for the numbers greater than the average. How do I get it to print the average for all 10 numbers and which numbers are greater than the average?
import java.util.Scanner;
public class MeanAndHigher
{
public static void main(String [] args)
{
Scanner reader = new Scanner(System.in);
Number[]numbers = new Number[10];
Number[] greaterList = new Number[10];
int sum = 0;
int greaterListIndex = 0;
for (int i = 0; i < numbers.length; i++)
{
System.out.println("Enter a number: ");
int a = reader.nextInt();
sum += a ;
{
double average= sum/10;
if (a > average)
greaterList[greaterListIndex]=numbers[i];
greaterListIndex++;
System.out.println("The average is: " + average);
}
}
System.out.println("Numbers greater than the average are: ");
for( int i = 0; i < greaterListIndex; i++) {
System.out.println(greaterList[i]);
}
}
}
Aucun commentaire:
Enregistrer un commentaire