While loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

While loop

package com.company; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int a = scanner.nextInt(); while (a!=0) { if (a == 1) { System.out.println("1"); scanner.nextInt(); } else if (a == 2) { System.out.println("2"); } else if (a == 3) { System.out.println("3"); } else if (a == 4) { System.out.println("4"); } else { System.out.println(""); } } System.out.println("Exit"); } } What`s the problem?

27th Mar 2021, 7:06 AM
Par9shiy
Par9shiy - avatar
1 Answer
+ 2
You can't declare your own packages in sololearn playground. So remove the first line. First of all, you don't need the while loop. Remove it or else the program will run infinitely and crash if the input is not 0. You can also use a switch case construct instead of if - else if chains. https://code.sololearn.com/cvJmOd1Kv6cG/?ref=app
27th Mar 2021, 7:15 AM
Soumik
Soumik - avatar