-1

Here is my while loop. The program sums up integers until a negative number is input. At that point the loop should break and it should print "Goodbye". However it is adding the negative number each time before it says goodbye. Im not sure what is going wrong here. Please help?!

import java.util.Scanner;

public class While {
    public static void main(String[] args)
    {
        int input = 5;
        int sum = 0;

        while(input >= 0)
        {
            System.out.println("Please enter a positive integer: ");
            Scanner in = new Scanner (System.in);
            input = in.nextInt();
            sum = sum + input;
            System.out.println("Running total: " + sum );
        }

        System.out.println("Goodbye!" );
    }

Test:

Please enter a positive integer: 

5

Running total: 5

Please enter a positive integer: 

10

Running total: 15

Please enter a positive integer: 
-1

Running total: 14

Goodbye!

I do not want to get the return value of 14, it should simply say Goodbye!

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437

2 Answers2

1

You need to use the break keyword. Your loop will always finish so the check on the while only happens after you've added the negative number. You could change to this:

        while(true)
        {
            System.out.println("Please enter a positive integer: ");
            Scanner in = new Scanner (System.in);
            input = in.nextInt();
            if(input <0){
                break;
            }
            sum = sum + input;
            System.out.println("Running total: " + sum );
        }

Or this:

while(input >= 0)
            {
                System.out.println("Please enter a positive integer: ");
                Scanner in = new Scanner (System.in);
                input = in.nextInt();
                if(input <0){
                    break;

                    sum = sum + input;
                    System.out.println("Running total: " + sum );
                }
            }

Or to avoid if statements entirely if needed (though that isn't the point of loops):

while(input >= 0)
                {
                    sum = sum + input;
                    System.out.println("Please enter a positive integer: ");
                    Scanner in = new Scanner (System.in);
                    input = in.nextInt();

                    System.out.println("Running total: " + sum );

                }
Joe W
  • 2,773
  • 15
  • 35
  • So there is no way to make this work without the use of if statements? I thought the point of while loops was not to use if statements. If I wanted to use if statements I could eliminate the while loop entirely. – Sean Flanagan Jun 20 '18 at 16:59
  • Added a third option avoiding the use of if statements. However, the point of while loops is not the avoidance of if statements. I think you would find you need the loop no matter what even if you do use if statements – Joe W Jun 20 '18 at 17:05
  • The third solution you entered does not add the integers to the current sum, it only prints out the integer. However it did help me to find the solution: – Sean Flanagan Jun 20 '18 at 17:12
  • The addition is in the third solution. First line in the loop. – Joe W Jun 20 '18 at 19:01
1

Here was my solution:

while(input >= 0)
        {
            sum = sum + input;
            System.out.println("Running total: " + sum );
            System.out.println("Please enter a positive integer: ");
            Scanner in = new Scanner (System.in);
            input = in.nextInt();

        }
        System.out.println("Goodbye!" );
    }

by calculating the sum at initialization and before the first integer is entered. It appears to work the way I want now. Thanks for your help.