+ 1

I don't understand why my Javas witch statement won't work & possibility of cases other than numbers in switch statements

Hello guys, I wrote a switch statement (see below), and as far as I can tell, it's written correctly so I don't understand what I did wrong, even with the explanation of the error that comes up. Like I think the error is saying that a semicolon should go between the closing parentheses or ")"and the opening squiggly brace or "{" in the first line of the switch statement, but a ; shouldn't go there. The 2nd error is an orphaned case, but I don't know what that means. And while I'm here, can you have a case that is anything other than a number? Like: switch (tagger) { case ana: System.out.println("Ana is it."); Below are the code, the errors and a link to the code. First the code: public class Program { public static void main(String[] args) { int medal = 1; Switch (medal) { case 1: System.out.println("You got 1st place."); case 2: System.out.println("You got 2nd place."); case 2: System.out.println("You got 3rd place"); } } } Next the Errors: ./Playground/Program.java:10: error: ';' expected Switch (medal) { ^ ./Playground/Program.java:11: error: orphaned case case 1: ^ 2 errors Link to the code: https://code.sololearn.com/cBOSiaYx4nFe Thank you in advance for your help! Thanks, Iulia

26th Aug 2022, 2:00 PM
Iulia
3 Antworten
+ 2
Use small s in switch( ...) { and you have same case values last as 2. Must be unique cases. Add a break; after each case, otherwise it fall through the next all cases from matching case, executes automatically.. hope it helps....
26th Aug 2022, 2:13 PM
Jayakrishna 🇮🇳
0
public class Program { public static void main(String[] args) { printDays(-2); } public static void printDays(int days){ switch(days){ case 0: System.out.println("today is sunday"); break; case 1: System.out.println("today is Monday"); break; case 2: System.out.println("today is Tueday"); break; case 3: System.out.println("today is Wednesday"); break; case 4: System.out.println("today is Thursday"); break; case 5: System.out.println("today is Friday"); break; case 6: System.out.println("today is Saturday"); break; default: System.out.println("Invalid day"); break; } } }
26th Aug 2022, 2:04 PM
Harsh Nama
Harsh Nama - avatar
0
switch (medal) { // small letter case 1: System.out.println("You got 1st place."); break; case 2: System.out.println("You got 2nd place."); break; case 3: // 3rd place System.out.println("You got 3rd place"); break; default: System.out.println("You got " + medal); }
26th Aug 2022, 2:10 PM
SoloProg
SoloProg - avatar