+ 1

Checking why the output of a Java Switch statement is 2 lines instead of 1

Hello guys, In the code below the output is two lines when I think it should only be one. My code below is actually part of longer code, so here is a link to the full code: https://code.sololearn.com/cBOSiaYx4nFe but here is the output for this section of the code: You got the sliver medal. You got the bronze medal. So is it because I put case 3 before case 2? And it printed out case 2, because that was the 3rd case, but also case 3, because it matched the case of the medal number? medal = 3; switch (medal) { case 1: System.out.println("You got the gold medal."); break; case 3: System.out.println("You got the bronze medal."); break; case 2: System.out.println("You got the silver medal."); break; }

26th Aug 2022, 2:43 PM
Iulia
4 Réponses
+ 1
I only got 1 line assuming that it is int medal = 3 Could you please LINK your code so we can test it? Go to Code section, click +, select the programming language, insert your code, save. Come back to this thread, click +, Insert Code, sort for My Code Bits, select your code.
26th Aug 2022, 2:50 PM
Lisa
Lisa - avatar
+ 1
You have 4 switch blocks: 1st : medal = 1 so prints first 3 lines because case 1 match, no break statement , so fall through until end or break. 2nd : medal = 2 so prints got silver medal 3rd: medal = 3 Prints bronze medal 4th: prints default case you can continue in thread previously : https://www.sololearn.com/discuss/3077554/?ref=app
26th Aug 2022, 3:04 PM
Jayakrishna 🇮🇳
+ 1
Oh my God, I miss the most obvious things. I'm sorry, I can't believe I thought I had 3 switch statements.
26th Aug 2022, 3:17 PM
Iulia
0
Hello Lisa, I updated my post but my code is actually part of longer code, so I wonder if that has anything to do with it. Here is a link to the full code: https://code.sololearn.com/cBOSiaYx4nFe
26th Aug 2022, 3:01 PM
Iulia