+ 1

Why can't use " case 65"

import java.util.*; class Example{ public static void main(String args[]){ Scanner input=new Scanner(System.in); System.out.print("Input an integer : "); int num=input.nextInt(); switch(num){ case 1 : System.out.println("Number 1 ");break; case 'A': System.out.println("A");break; case 'B': System.out.println("B");break; case 65 : System.out.println("65");break; //Illegal default : System.out.println("Wrong Number");break; } } }

24th Dec 2020, 10:47 AM
Sachindu Kavimina
Sachindu Kavimina - avatar
1 Respuesta
+ 6
The int value of "A" in the ASCII code is 65; so when you try " case 'A' " you are really doing case 65. So, the problem is that you have the same case two times in a switch.
24th Dec 2020, 10:54 AM
seroco
seroco - avatar