Why doesn't my “While Loop” print the computation of finding the average “score”?

谁说我不能喝 提交于 2019-12-02 09:31:54

问题


I am writing a program that reads a sequence of positive integers input by the user. User will only enter one integer at a time.Then it will compute the average of those integers. The program will end when user enters 0. (0 is not counted in the average).The program will print out the average once the program ends.

Question: My code stops working when I gets to the while loop hence it doesn't compute the input by user, hence prints out nothing. Why doesn't my while loop compute the average from the user's inputs? Appreciate your guidance :)

import java.util.Scanner;

public class AverageOfIntegers {

    public static void main(String[] args) {

        int integer;
        double sum;
        sum = 0;
        double average;
        Scanner input = new Scanner(System.in);
        int count; count = 0; 
        average = 0;


        System.out.println("Please enter an integer: ");

        integer = input.nextInt();


        while (integer != 0) {
        count = count + 1;  

            sum = sum + integer; 

            average = sum / count;

        }

        System.out.println("Average = " + average);

    }

}

回答1:


This is because you are never actually summing over more than one integer. The user only ever enters one number. As a result your loop is essentially acting on just the one number. You need to put the input inside the while loop and save a running sum and count there. Something more like this

while (integer != 0) {
    count += 1;  

        sum += integer; 

        average = sum / count;
        integer = input.nextInt();
    }



回答2:


Explanation

First of all, when you define data types, you can set their default value in the definition. Ex:

double sum = 0;

vs

double sum;
sum = 0;

Secondly, sum = sum + integer; is the same as: sum += integer;

Thirdly, count = count + 1; is the same as: count += 1 OR (and better yet), count++;

As for your actual algorithm, there is one problem and one suggestion:

  1. you are not changing integer's value after each loop. So, you can either do that in the while condition: while ((integer = input.nextInt()) != 0) { or, at the end of each loop:

    while (integer != 0) {
        count ++; 
        sum += integer;
        average = sum / count;
        integer = input.nextInt();
    }
    
  2. This is a suggestion for technically better code (in my opinion), but it looks better, is more intuitive and requires less calculations to calculate the average after the while loop is done instead of during. That way, you only calculate it once, where needed, vs. every loop, which is not needed.

________________________________________________________________________________

The Code (complete class)

public class AverageOfIntegers {
    public static void main(String[] args) {
        int integer;
        double sum = 0;
        double average = 0;
        Scanner input = new Scanner(System.in);
        int count = 0;

        System.out.println("Please enter an integer: ");

        // set integer = to the nextInt() while looping so it calculates properly
        while ((integer = input.nextInt()) != 0) {
            count ++; 
            sum += integer;
        }
        average = sum / count; // calculate the average after the while-loop

        System.out.println("Average = " + average);
    }
}

________________________________________________________________________________

Example input/output:

Please enter an integer:

5

10

15

0

Average = 10.0

So it did 5 + 10 + 15 = 30 (which is the sum), and then the average is 30 / 3 (30 is the sum, 3 is the count), and that gave you Average = 10.0.




回答3:


You need to move integer = input.nextInt(); inside the loop, so your program will collect inputs in a loop. See the corrected version:

import java.util.Scanner;

public class AverageOfIntegers {

    public static void main(String[] args) {

        int integer = 0, count = 0; 
        double sum = 0.0, average = 0.0; 
        Scanner input = new Scanner(System.in);

        System.out.println("Please enter an integer: ");
        integer = input.nextInt();

        while (integer != 0) {
            count = count + 1;  
            sum = sum + integer; 
            System.out.println("Please enter an integer: ");
            integer = input.nextInt();
        }

        average = sum / count;
        System.out.println("Average = " + average);
    }
}



回答4:


The problem is that the input.nextInt() should be part of the loop. The way you wrote it, the code gooes into an infinite loop whenever the first input is non-zero. Instead, do:

while ((integer = input.nextInt())  != 0) {
  count = count + 1;  
  sum = sum + integer; 
  average = sum / count;
}



回答5:


In the loop:

while (integer != 0) {
    count = count + 1;  

    sum = sum + integer; 

    average = sum / count;
}

This will only stops when integer is 0, but this variable is not changing in the loop, so it will never be 0 if it wasn't already in the first place.

According to what you said you want to do, you should probably repeat the call to integer = input.nextInt(); inside your loop, lke this:

System.out.println("Please enter an integer: ");
integer = input.nextInt();

while (integer != 0) {

    count = count + 1;  
    sum = sum + integer; 

    System.out.println("Please enter an integer: ");
    integer = input.nextInt();
}

average = sum / count;

Also, as others have said, you only need to compute the average once after the loop, so I moved it too.



来源:https://stackoverflow.com/questions/19727896/why-doesnt-my-while-loop-print-the-computation-of-finding-the-average-score

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