简体   繁体   中英

passing user inputs to methods are not being added up correctly

I'm trying to write a program that asks the user for numbers until the user enters -1. The program will then provide the sum of the numbers. The program should use a Statistics object to calculate the sum.

There are, however, specifications that come with this project. Those are

Do not modify the Statistics class in this part. Instead, implement the program for calculating the sum by making use of it.

public class Statistics {

    private int count;
    private int sum;
    //private double average;

    public Statistics() {
        this.count = 0;
    }

    public void addNumber(int number) {
        //sum += number;
        count++;
    }

    public int getCount() {
        return this.count;
    }

    public int sum(int user) {
        this.sum = this.sum + user;
        return this.sum;
    }

    public double average() {
        double average;
        average = (double) sum / this.count;
        return average;
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        Statistics statistics = new Statistics();
        System.out.println("Enter Numbers:");
        int user=0;
        while (user >= 0) {
            statistics.sum(user = scan.nextInt());
        }
        System.out.println("Sum: " + statistics.sum(user));
    }
}

If I (the user) input 4,2,5,2,-1... the program outputs Sum: 11 . Why does it not equal Sum: 15 ?

Pay attention to this snippet:

 int user=0;
    while (user >= 0) {
        statistics.sum(user = scan.nextInt());
    }
    System.out.println("Sum: " + statistics.sum(user));
  1. you enter 4 -> .sum adds 4 to 0. sum=4; user = 4 (user>=0 true) sum=4; user = 4 (user>=0 true)
  2. you enter 2 -> .sum adds 2 to 4. sum=6; user = 2 (user>=0 true); sum=6; user = 2 (user>=0 true);
  3. you enter 5 -> .sum adds 5 to 6. sum=11; user = 5 (user>=0 true); sum=11; user = 5 (user>=0 true);
  4. you enter 2 -> .sum adds 2 to 11. sum=13; user = 2 (user>=0 true); sum=13; user = 2 (user>=0 true);
  5. you enter-1 -> .sum adds -1 to 13. sum=12; user =-1 (user>=0 false); sum=12; user =-1 (user>=0 false);

and your user variable is -1 at this time.

Finally you call statistics.sum(user) and that adds -1 to 12, which is 11 .

This question is from mooc.fi's Java Programming Part 4 Programming exercise "Statistics".

In part 2 you should implement the class Statistics .
Below it contains a part that shows how this class is used:

 public class Main { public static void main(String[] args) { Statistics statistics = new Statistics(); statistics.addNumber(3); statistics.addNumber(5); statistics.addNumber(1); statistics.addNumber(2); System.out.println("Count: " + statistics.getCount()); System.out.println("Sum: " + statistics.sum()); System.out.println("Average: " + statistics.average()); } }

The program prints the following:

 Count: 4 Sum: 11 Average: 2.75

Before moving to part 3, make sure your Statistics class produces the right output when used with that Main.

Once you got this working, move to the next part.

If I (the user) input 4,2,5,2,-1... the program outputs Sum: 11. Why does it not equal Sum: 15?

It is happening because you are calling statistics.sum at two places (one, inside the while loop and one, after the while loop). The following code will help you trace how it is working:

import java.util.Scanner;

public class Statistics {

    private int count;
    private int sum;
    // private double average;

    public Statistics() {
        this.count = 0;
    }

    public void addNumber(int number) {
        // sum += number;
        count++;
    }

    public int getCount() {
        return this.count;
    }

    public int sum(int user) {
        System.out.println("Got " + user);// Add this line for tracing 
        this.sum = this.sum + user;
        return this.sum;
    }

    public double average() {
        double average;
        average = (double) sum / this.count;
        return average;
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        Statistics statistics = new Statistics();
        System.out.println("Enter Numbers:");
        int user = 0;
        while (user >= 0) {
            statistics.sum(user = scan.nextInt());
        }
        System.out.println("Sum: " + statistics.sum(user));
    }
}

A sample run:

Enter Numbers:
4
Got 4
2
Got 2
5
Got 5
2
Got 2
-1
Got -1
Got -1
Sum: 11

Ths, the final result has been calculated as 4 + 2 + 5 + 2 + (-1) + (-1) = 11

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