Lukas Doppler
Lukas Doppler

Reputation: 21

How can i calculate the sum correctly without using sum++;?

So I'm working on a project currently and this step asked me to write a program which asks for numbers and when "-1" is entered, it will calculate the sum of all numbers entered except the -1. I simply fixed this by adding +1 after the loop, but im sure there is another "proper" way of doing it and i would like to learn how.

Any help is appreciated. ( Note: I've only been learning Java for around a week so ELI5 please )

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 numbertyped = 0;
            int sum = 0;

            System.out.println("Type numbers: ");

            while (numbertyped != -1) {

                numbertyped = Integer.parseInt(reader.nextLine());

                sum = sum + numbertyped;

            }
            sum++;
            System.out.println("Thank you and see you later!");
            System.out.println("The sum is " + sum);

EDIT: My program is complete now. I used the solution of adding a break in the while loop and after adding the rest of the features i wanted, this is the end product: ( if anyone has tips on how to improve my code or make it more efficient please comment! )

    import java.util.Scanner;

    public class LoopsEndingRemembering {

        public static void main(String[] args) {
            // program in this project exercises 36.1-3


        // actually this is just one program that is split in many parts

        Scanner reader = new Scanner(System.in);

        int numbertyped = 0;
        int sum = 0;
        int howmany = 0;
        int evencounter = 0;
        int oddcounter = 0;

        System.out.println("Type numbers: ");

        while (true) {

            numbertyped = Integer.parseInt(reader.nextLine());

            if (numbertyped == -1) 
            {
                break;
            }

            if (numbertyped % 2 == 0)

            {
                evencounter++;
            } 

            else 

            {
                oddcounter++;
            }

            sum = sum + numbertyped;
            howmany++;

        }

        double average = (double) sum / howmany;

        System.out.println("Thank you and see you later!");
        System.out.println("The sum is " + sum);
        System.out.println("How many numbers: " + howmany);
        System.out.println("Average: " + average);
        System.out.println("Even numbers: " + evencounter);
        System.out.println("Odd numbers: " + oddcounter);

    }
}

Upvotes: 0

Views: 283

Answers (3)

Elliott Frisch
Elliott Frisch

Reputation: 201517

You can terminate your loop as soon as -1 is entered, change

while (numbertyped != -1) {
    numbertyped = Integer.parseInt(reader.nextLine());

to something like

while ((numbertyped = Integer.parseInt(reader.nextLine())) != -1) {
    // ...

and the loop body will not be entered when -1 is assigned to numbertyped.

Based on your edits, I would suggest you might shorten sum = sum + numbertyped; to sum += numbertyped; and that you can calculate howmany by summing evencounter and oddcounter. Like,

System.out.println("Type numbers: ");
while (true) {
    numbertyped = Integer.parseInt(reader.nextLine());
    if (numbertyped == -1) {
        break;
    }
    if (numbertyped % 2 == 0) {
        evencounter++;
    } else {
        oddcounter++;
    }
    sum += numbertyped;
}
int howmany = evencounter + oddcounter;
double average = (double) sum / howmany;

Upvotes: 8

D M
D M

Reputation: 1410

I think in this case, you can just reverse the order of the statements in the while loop.

        int numbertyped = 0;
        int sum = 0;

        System.out.println("Type numbers: ");

        while (numbertyped != -1) {

            sum = sum + numbertyped;

            numbertyped = Integer.parseInt(reader.nextLine());

        }

The first time through the loop, numberTyped is 0, which is perfect - it won't exit the loop, and it won't actually add anything to the sum. After that, it won't change the sum until after you've checked whether it's -1.

Upvotes: 0

Alexis Wilke
Alexis Wilke

Reputation: 20818

Quite often, C-like language users do not thing of using an "exit loop" (break) in similar situations. You'd want something like this instead:

    while (true)
    {

        numbertyped = Integer.parseInt(reader.nextLine());

        if(numbertyped == -1)
        {
            break;
        }

        sum = sum + numbertyped;
    }

As a side note, a "forever loop" can be written with a for(;;) which some people say is better than a while(true). However, most people view while(true) as easier to read. Personally I use the for(;;). Either way both are optimized properly and thus you will get no runtime difference.

Reference about for(;;): while (1) Vs. for (;;) Is there a speed difference?

Upvotes: 4

Related Questions