user5433097
user5433097

Reputation:

Find average of numbers (with while loops; java)

My code stops when the user inputs -1 (like it should) but the average printed is wrong.

This is what I have:

import static java.lang.System.*;
import java.util.Scanner;
import java.util.*;

class averages_1
{
    public static void main(String[] args)   
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the scores:");
        double score = 0;
        double num = 0;
        double sum = 0;

    while (score != -1)
    {
        score = scan.nextDouble();
        sum += score;
        num++;
    }
    System.out.println("The average is:" + (sum/num));
}

}

If you enter 50 then 105 then -1, the output is

Enter the scores:

50

105

-1

The average is: 51.333333333333336

I just need to correct the average. Any help is appreciated!

Upvotes: 1

Views: 25153

Answers (6)

user1702875
user1702875

Reputation: 29

Ok so then just use this

while (score != -1) {
        if (scope != -1){
           sum += score;
           score = scan.nextDouble();
           num++;
        }
    }

It's not pretty, but like everyone else is telling you, don't add -1 to score since its your escape character.

Upvotes: 0

Iqbal S
Iqbal S

Reputation: 1154

Change your code as below

import static java.lang.System.*;
import java.util.Scanner;
import java.util.*;

class averages_1 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the scores:");
        double score = 0;
        double num = 0;
        double sum = 0;
        while (score != -1) {
            sum += score;
            score = scan.nextDouble();
            num++;
        }
        num--;
        System.out.println("The average is:" + (sum/num));
    }
}

Upvotes: 0

You are increasing num even for input==-1, which is not a valid input, but a escape character...

Either modify the logic or just correct the snippet:

System.out.println("The average is:" + (++sum/--num));

Upvotes: 0

nafas
nafas

Reputation: 5423

you can read input within the condition of your while loop instead, this way as soon as you read -1 you quit the loop and it won't be added to your sum variable

while ((score=scan.nextDouble()) != -1)
{
    sum += score;
    num++;
}

Upvotes: 5

Eran
Eran

Reputation: 393781

You include the -1 in your average.

One way to avoid it is to add another condition :

while (score != -1)
{
    score = scan.nextDouble();
    if (score != -1) {
        sum += score;
        num++;
    }
}

However, a more elegant solution would read the first input before the loop, and only add valid inputs :

score = scan.nextDouble();
while (score != -1)
{
    sum += score;
    num++;
    score = scan.nextDouble();
}

In general, changing the variable used in the condition of the while loop should be the last thing you do in the iteration.

Upvotes: 7

Aron_dc
Aron_dc

Reputation: 863

You just add the last -1 to your sum and still count it as number. You have to break; your loop if your input was -1.

Upvotes: 3

Related Questions