Reputation: 3
i'm a newbie to java so i decided to create a simple program that prints out even numbers. I tried to make it exit a while loop if you answered no to the question but it just keeps on going.
Here's the code
public static void main(String[] args){
String continueYorN = "Y";
int i = 0;
while (continueYorN.equalsIgnoreCase("y")){
while(i >= 0){
i++;
if((i%2) == 0){
System.out.println(i);
continue;
}
System.out.println("Do you want to generate another even number?");
continueYorN = userInput.nextLine();
}
}
}
Upvotes: 0
Views: 114
Reputation: 1364
Your loop has no break condition
(i.e, something that stop the loop in some condition), so it will continue forever.
You should replace the inner while
with something like that:
while(i >= 0){
i++;
if((i%2) == 0){
System.out.println(i);
break;
}
Upvotes: 1