0
How do I ask for values from the user in Switch condition?
I used Scanner but it has a different data type than int. So switch is not accepting it. I then tried equating it with an int variable but of course it's not possible.. Plz help
2 Antworten
+ 14
► Take input using the scanner class and store it in a variable and use that variable to check cases of switch statements.
► You can refer this for better understanding :
Scanner in = new Scanner(System.in);
System.out.println("Enter Your Choice : ");
int ch = in.nextInt();
switch (ch) {
case 1:
.........................;
break;
case 2:
.........................;
break;
default:
.........................;
break;
}
+ 3
Rishabh Doshi
Scanner myObj = newScanner(System.in);
Int myInt = myObj.nextInt(); should give you an int.