Reputation: 23
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.
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
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
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
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);
}
}
}
}
Upvotes: 1
Views: 5575
Reputation: 811
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);
}
}
Upvotes: 1
Reputation: 91
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.
Upvotes: 2
Reputation: 871
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.
Upvotes: 0