简体   繁体   中英

Issue with User Input and Sum within a Java Loop

Before I begin, I want to thank you all for the help. I would recommend not giving me the code but instead point me to where the mistakes are. Thanks!

I've been trying to figure this issue out for the past few hours and I can't seem to see where I went wrong. I am trying to continue to ask for user input until the user enters -1, which is the sentinel value. After the sentinel value is entered, the program displays the integers entered by the user and their sum. For reasons unknown to me, the printed value is only -1 and the sum is completly off. Here's my code:

import java.util.Scanner;

public class UserSum
{
    public static void main(String args[])
    {
        //prompt user to enter numbers
        Scanner userInput = new Scanner(System.in);
        System.out.print("Enter positive integers. Enter -1 to stop. ");
        int integers = userInput.nextInt();

        //sum is initially set to 0
        int sum = 0;

        //execute commands as long as the input does not equal -1
        while(integers != -1)
        {
            //keep gathering user input
            integers = userInput.nextInt();
            sum += integers;
        }

        //print the results to the user
        System.out.println("You entered: " + integers + ", ");
        System.out.println("Sum is: " + sum);
    }
}

The reason your sum value outputs incorrectly is because you read in two inputs before adding to the sum. One read is done entirely before the while loop and one is done before adding to the sum in the while loop. The first value you enter will never be saved in the sum.

Try to implement something similar to the approach below...

int sum = 0;
int value = 0;

while (value != -1) {
   sum += value;
   value = input.nextInt();
   sysout(value);
}

sysout(sum);

An ArrayList is a structure that is greats for all intents and purpose when trying to store multiple values. Google is your friend, ask Google to do you a favor and you'll be on your way with ArrayLists. In your loop, you can then add it, storing all of them.

You called it integers but it is actually not a list of integers, just the last one read. Check also where you sum up. You do no sum up the first read number, but you sum up the last (-1)... easily to see, because you have two lines where you read the input, that is already suspicious (see the other answer with the loop).

As per your request I will not post code to help but offer advice on ways you can tackle this.

First off, the way that you handle user inputs is off to a great start, but it might not be 100% correct. I think the 'while' loop is fine. The problem is that you are reading the 'first int', not adding it to the sum, and then reading the 'next int' before you increment the sum by said 'int'.

So in turn I would recommend switching the integers and sum lines in the while loop or initially declaring sum to be equal to integers.

As far as printing all the integers used, you will have to use an array or an array list. You would first determine the number of ints entered by the user, excluding '-1', and declare an array based on that. Then you would read all the integers into the array and sum them at then end. In general I think this would be a more effective method of tackling this.

I hope this helps!

Without arrays

If not using an array, you can use a string and append the strings with the integers that are used to calculate sum

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