JohnnyC
JohnnyC

Reputation: 67

Using an if statement with a while loop?

I'm trying to do this question:

Design a program that reads in a list of numbers terminated by -9999 and prints out the sum entered.

I am amending the program so that only values between 1 and 100 are summed, and as you can see I've tried to implement an if statement. But, nothing has changed and this part has stumped me. I've been stuck on it for ages, code follows:

import java.util.Scanner;

public class Summing {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int Number, Sum = 0;

        System.out.println("Enter a list of whole numbers, finished by -9999");
        Number = sc.nextInt();

        if (Number > 0 || Number < 100);
        while (Number != -9999) {
            Sum = Sum + Number;
            Number = sc.nextInt();
        }

        System.out.println("Sum is " + Sum);
    }
}

Upvotes: 2

Views: 91

Answers (4)

TMS
TMS

Reputation: 759

Your if is BEFORE the while loop. Put it IN the loop.

Upvotes: -1

The111
The111

Reputation: 5867

Move the if statement inside of the loop. Right now the if statement is only being executed once before entering the loop (and it is doing nothing, thanks to the ; immediately following the condition). You want it to be used on every value.

Also notice I changed the || to an &&. Otherwise for example 101 would match, which is not between 0 and 100. Finally, a = a + b can always be written more succinctly as a += b.

while (Number != -9999) {
    if (Number > 0 && Number < 100) {
        Sum += Number;
    }
    Number = sc.nextInt();
}

As another stylistic change (which is pretty much universal convention) you should consider changing your variables to lowercase names.

Upvotes: 4

Elliott Frisch
Elliott Frisch

Reputation: 201409

You were fairly close, but the if test should be in the loop (and you should probably use { and } braces). By convention Java variable names start with a lower case letter, and I would prefer the += operator for adding to the sum. Also, to check your range you logically need an and (not an or). Something like,

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter a list of whole numbers, finished by -9999");
    int number = sc.nextInt();
    int sum = 0;
    while (number != -9999) {
        if (number > 0 && number < 100) {
            sum += number;
        }
        number = sc.nextInt();
    }
    System.out.println("Sum is " + sum);
}

Upvotes: 1

janos
janos

Reputation: 124648

You placed the if before the while loop, and it has an empty (= does nothing at all). The condition was also wrong anyway as Number > 0 || Number < 100 will match all numbers. You want to change the || to &&.

This should work:

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int Number, Sum = 0;

    System.out.println("Enter a list of whole numbers, finished by -9999");
    Number = sc.nextInt();

    while (Number != -9999) {
        if (1 < Number && Number < 100) {    
            Sum += Number;
        }
        Number = sc.nextInt();
    }

    System.out.println("Sum is " + Sum);
}

Upvotes: 1

Related Questions