What's the problem with cases ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What's the problem with cases ?

package main; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner X = new Scanner(System.in); char input; System.out.print(" Enter Any Character : "); input = X.next().charAt(0); switch (input) { case 'm' || 'M' : System.out.println("Male "); case 'f' || 'F': System.out.println("Female"); default : System.out.println("Enter a Valid character"); } } } https://code.sololearn.com/WB04hna20SyY/?ref=app https://www.sololearn.com/discuss/3193143/?ref=app

22nd Feb 2023, 2:20 AM
Aizaz Khalid
Aizaz Khalid - avatar
1 Answer
+ 6
Switch's cases are not boolean compatible operands. You can't use logical operators to test them. Chained cases are written in sequence, with colon after each case case 'F' : case 'f' : // code here break; Don't forget to set a case's block end by issuing `break` before defining other cases, except for the last label. If you don't put `break` in, you may find that the cases after may be executed unintentionally. Next time, please don't attach other people's code or post, they're irrelevant to your question.
22nd Feb 2023, 2:40 AM
Ipang