问题描述
我在做一个程序,你必须要找到的平均用户已经进入了10个整数,然后让程序告诉我输入的用户有多少数字实际上是高于平均水平,那么实际打印这些数字。我与它告诉我是什么上面,他们平均和数字的问题。它计算的平均,而不是之后,它只是不断给我这样的事情在输出
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
有0号高于平均水平
这些数字:0
There are 0 numbers above the averageThose numbers are: 0
我在做什么错了?
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);
}
}
推荐答案
您如果
块是由分号终止;你可以解决它像这样
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);
}
修改
在审查您的code的休息,我觉得你真的需要像(使用)
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);
编辑2
全部放在一起,
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);
}
这篇关于Java的平均,高于平均水平的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!