Java Average and Above Average

别等时光非礼了梦想. 提交于 2019-12-25 01:24:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!