1

This is for help with a homework assignment, but the site won't let me tag it as such. FYI: I haven't learned loops besides the while type yet. I need some help with a while loop counting program for Java. I'm new to programming, so I'll try to explain this as best I can. The program will ask the user for a starting number, ending number, and increment. I want it to be able to execute 4 types of scenarios. The first three types work fine, but the last scenario infinitely loops. How can I change my code to meet all 4 types of scenarios? Any explanations or help is appreciated. Below are the scenarios and after them is my code.

  1. starting number < ending number, counts up by increment to ending number and stops: Where do you want to start counting? 35 How far do you want me to count? 60 Increment? 5 COUNTING 35 40 45 50 55 60

  2. starting number > ending number, starting number counts down by increment to ending number and stops: Where do you want to start counting? 44 How far do you want me to count? -22 Increment? 11 COUNTING 44 33 22 11 0 -11 -22

  3. Starting number = ending number, only that number is outputted: Where do you want to start counting? 99 How far do you want me to count? 99 Increment? 3 COUNTING 99

  4. The increment does not evenly count up or down to to the ending number. It's supposed to stop before it reaches the ending number: Where do you want to start counting? 23 How far do you want me to count? 46 Increment? 6 COUNTING 23 29 35 41

import java.util.Scanner;

public class Count {

public static void main(String[] args) {

    int counter, limit, inc;

    Scanner input = new Scanner(System.in);

    System.out.println("Where do you want to start counting?");
    counter = input.nextInt();

    System.out.println("How far do you want me to count?");
    limit = input.nextInt();

    System.out.println("Increment?");
    inc = input.nextInt();

    System.out.println("COUNTING");
    System.out.println(counter);

    while (counter != limit) {
        if (counter < limit) {
            counter = counter + inc;
            System.out.println(counter);

        }
        else if (counter > limit) {
            counter = counter - inc;
            System.out.println(counter);

        }

    }

}

}

3 Answers 3

2

you can use break in your while loop, if you want.

while (counter != limit) {
    if (counter < limit) {
        if (counter + inc <= limit){
            counter = counter + inc;
            System.out.println(counter);
        } else {
            break;
        }
    }
    else if (counter > limit) {
        if (counter - inc >= limit) {
            counter = counter - inc;
            System.out.println(counter);
        } else {
            break;
        }
    }
}

break will stop the loop and continue on the code after the loop.

Sign up to request clarification or add additional context in comments.

Comments

1

You say:

 while (counter != limit) {
    if (counter + inc < limit) {
        counter = counter;
        System.out.println(counter);

    } else if (counter - inc > limit) {
        counter = counter;
        System.out.println(counter);

    }

This tells the computer to keep running if counter != limit. counter is almost certain to never == limit. Imagine this: limit = 43. If you increment by 6, there is only a 1 in 6 chance that the number you started from will become 43. (42-38 + 6 != 43), (37 + 6 == 43). There are so many ways to keep counting without ever having counter == limit (to break out of the while loop).

Try putting your while loop inside the if statement:

//edited to resolve issue brought up in comments
if (counter < limit) {
    while (counter < limit + inc) {
        //until counter is bigger than limit, count up by inc
        counter += inc;
        System.out.println(counter);
    }
} else if (counter > limit) {
    while (counter > limit - inc) {
        //until counter is smaller than limit, count down by inc
        counter -= inc;
        System.out.println(counter);
    }
}

3 Comments

Thanks, this code definitely stops the infinite looping. The problem I'm having now is when the increment doesn't evenly reach the limit, its addition or subtraction (depending on the scenario) goes beyond the limit once. I'll keep working on it, but at least it stops.
@user3404250 Ah, I see it now. When you begin the while loop for the final time (the problem), it is true. Then it adds inc and turns it false, but must finish by printing counter. So, you need to do while (counter < limit + inc) and while (counter > limit - inc)
that made it go twice past the limit (assuming I read your changes correctly), however it helped me figure out how to fix it. I got the correct output when I changed the first while to (counter + inc <= limit) and the second while to (counter - inc >= limit). Thanks for your help!
0

Instead of checking for inequality counter != limit, make two cases. It seems like you already understand if. There's one case where you check whether counter <= limit (counting up) and the case where we check whether counter >= limit (counting down). This should be enough for you, but I'm providing the code if you really want it. I'd encourage you to try on your own again before you look at it.

//(...)
System.out.println(counter);
boolean countingUp = counter <= limit; //if this variable is true, we are counting up
if (countingUp == true) {
    while(counter <= limit) {
        counter = counter + inc;
        System.out.println(counter);
    }
} else {
    while(counter >= limit) {
        counter = counter + inc;
        System.out.println(counter);
    }
}
//End of program.

1 Comment

Thanks, that does stop the infinite loop. However, it does count once past the limit in every scenario. I'll keep playing with it. It's nice to try some different methods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.