问题
I'm making a program where you have to find the average to the 10 integers that the user has entered and then get the program to tell me how many numbers the user entered were actually above average and then actually print those numbers. I'm have problems with it telling me what was above average and which numbers they were. Instead after it calculates the average it just keeps giving me something like this in the output
There are 0 numbers above the average Those numbers are: 0
What am I doing wrong?
public class Average {
static Scanner keyboard = new Scanner(System.in);
static int sum = 0, aboveAverage;
static double average;
public static void main(String[] args) {
int[] listOfInt = new int[10];// 10 integers MAX
System.out.println("Enter " + listOfInt.length + " integers: ");
for (int count = 0; count < listOfInt.length; count++) {
listOfInt[count] = keyboard.nextInt();
}
for (int count = 0; count < listOfInt.length; count++) {
sum = sum + listOfInt[count];
}
average = sum / listOfInt.length;// sum divided by 10
System.out.println("Average: " + average);
if (aboveAverage > average);
System.out.println("There are " + aboveAverage+ " numbers above the average");
System.out.println("Those numbers are: " + aboveAverage);
}
}
回答1:
Your if
block is terminate by the semi-colon; you could fix it like this
if (aboveAverage > average) { //;
System.out.println("There are "+aboveAverage+" numbers above the average");
System.out.println("Those numbers are: " + aboveAverage);
}
Edit
On reviewing the rest of your code, I think you really need something like (using the diamond operator <>)
double average = ((double) sum) / listOfInt.length;// sum divided by 10
System.out.printf("Average: %.2f%n", average);
List<Integer> aboveAverage = new ArrayList<>();
for (int v : listOfInt) {
if (v > average) {
aboveAverage.add(v);
}
}
System.out.printf("There are %d numbers above the average%n",
aboveAverage.size());
System.out.printf("Those numbers are: %s%n", aboveAverage);
Edit 2
Putting it all together,
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int[] listOfInt = new int[10];// 10 integers MAX
System.out.println("Enter " + listOfInt.length + " integers: ");
for (int count = 0; count < listOfInt.length; count++) {
listOfInt[count] = keyboard.nextInt();
}
int sum = 0;
for (int i : listOfInt) {
sum += i;
}
double average = ((double) sum) / listOfInt.length;
System.out.printf("Average: %.2f%n", average);
List<Integer> aboveAverage = new ArrayList<>();
for (int v : listOfInt) {
if (v > average) {
aboveAverage.add(v);
}
}
System.out.printf("There are %d numbers above the average%n",
aboveAverage.size());
System.out.printf("Those numbers are: %s%n", aboveAverage);
}
回答2:
System.out.print("How many numbers will you like to find the Average for:");
int values;
values = input.nextInt();
int counter = values;
int number;
int total = 0;
while (counter > 0) {
System.out.print("Please enter a number: ");
number = input.nextInt();
total = total + number;
counter = counter - 1;
}
average = ((double) total / values)
System.out.println("The Average is: " + average);
来源:https://stackoverflow.com/questions/26766273/java-average-and-above-average