How to keep the loop repeating if the user keeps entering a negative number in Java ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to keep the loop repeating if the user keeps entering a negative number in Java ?

System.out.println("PLease enter number of cake :"); cakeNo = scan.nextInt(); if (cakeNo < 0) { System.out.println("Please enter a positive number of cake :"); // outputs this if the user enters a negative number cakeNo = scan.nextInt(); } else { cakeTime = cakeTime * cakeNo; }

11th Feb 2019, 2:35 AM
Sharjeel Karim
Sharjeel Karim - avatar
4 Answers
+ 4
If you want a loop, you'll have to use a while instead of an if, and remove the else.
11th Feb 2019, 3:01 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
if-else statements are conditional statements which offer selection, not loops. They are not capable of looping your program.
11th Feb 2019, 3:09 AM
Hatsy Rei
Hatsy Rei - avatar
+ 1
thanks Hatsy Rei for the information 😃
11th Feb 2019, 3:10 AM
Sharjeel Karim
Sharjeel Karim - avatar
0
Hatsy Rei that I know but just wanted to know if “if-else” statement could be used or not
11th Feb 2019, 3:08 AM
Sharjeel Karim
Sharjeel Karim - avatar