简体   繁体   中英

Average user inputted numbers in Java

I am trying to get the average of user inputted numbers. Every time I input numbers the output is always-

The sum is 6 How many numbers: 4 Average: 2.0

A while loop is required, unfortunately for loops are not allowed. I am pretty new to Java so my formatting is sloppy. Where in my code are my problems?

import java.util.Scanner;

public class LoopsEndingRemembering {

    public static void main(String[] args) {
        // program in this project exercises 36.1-36.5
        // actually this is just one program that is split in many parts

        Scanner reader = new Scanner(System.in);
        int count = 0;
        int sum = 0;
        int endingVariable = -1;
        int input = 0;
        double average;

        while (input >= endingVariable) {
            System.out.println("Type numbers: ");
            input = Integer.parseInt(reader.nextLine());
            sum += count;
            average = (double)sum / count;
            count++;

            if (input == endingVariable) {
                System.out.println("Thank you and see you later!");
                System.out.println("The sum is " + sum);
                System.out.println("How many numbers: " + count);
                System.out.println("Average: " + average);
                break;
            } 
        }
    }
}

You have several errors here.

The main one that causes your error is sum += count . Instead of adding what the user has entered, you are adding the number that shows how many numbers there are. So it will always add 0+1+2+3 etc.

Another problem is that, once you change the above, you have to check whether the input == endingVariable before you add the input to the sum. So you have to write the if first, and then calculate the average inside it. Otherwise it will treat your -1 as part of the sequence of numbers to calculate.

You only need to add the input to the sum and increment the count if you find out that the input is not yet the same as endingVariable .

So:

  • Add the input, not the count, to the sum.
  • Calculate the average only when you have reached the final item.
  • Add the input to the sum and increment the counter only if your input is not endingVariable .

Try this one...

import java.util.Scanner;

public class LoopsEndingRemembering {

public static void main(String[] args) {
    // program in this project exercises 36.1-36.5
    // actually this is just one program that is split in many parts

    Scanner reader = new Scanner(System.in);

    int count = 0;
    int sum = 0;
    int endingVariable = -1;
    int input = 0;
    double average;



    while (input >= endingVariable) {


        System.out.println("Type numbers: ");
        input = Integer.parseInt(reader.nextLine());
        sum += input;   // you were doing mistake here.
        average = (double)sum / count;
        count++;

        if (input == endingVariable) {
            System.out.println("Thank you and see you later!");
            System.out.println("The sum is " + sum);
            System.out.println("How many numbers: " + count);
            System.out.println("Average: " + average);
            break;

        } 

    }

}
}

Two big issues with your code. First, this line:

sum += count;

It's probably just a typo, but it should be obviously:

sum += input;

Second, once you fix that, you will notice you are including the -1 in your sum. You probably want something like this:

    System.out.println("Type numbers: ");
    input = Integer.parseInt(reader.nextLine());

    if (input == endingVariable) {
        average = (double) sum / count;

        System.out.println("Thank you and see you later!");
        System.out.println("The sum is " + sum);
        System.out.println("How many numbers: " + count);
        System.out.println("Average: " + average);
        break;
    } else {
        sum += input;
        count++;
    }

Thank you for explaining in detail. It works fine now, all I had to do was what you said is change the input, and move the sum += input and count after the final item in an else. I was having the hardest time trying to figure out why my -1 was being included

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM