• Post Reply Bookmark Topic Watch Topic
  • New Topic

Switch statement  RSS feed

 
Yong Han
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just started learning java and i have no any idea about switch statement.
When i compile i got 16 errors and i dont know how to fix it .

public class Switch{
public static void main ( String[]args ){
int marks  =  10;
switch ( marks ){
case 10;
System.out.println( "Congratulation you have full marks" );
break;
case 9;
System.out.println( "Congratulation you almost have full marks" );
break;
case 8;
System.out.println( "Congratulation you have done very well" );
break;
case 7;
System.out.println("Congratulations you have done well");
break;
case 6;
System.out.println("You are doing alright but could study more");
break;
case 5;
System.out.println("You only got half marks, you need to do more work");}
break;
System.out.println("You got less than half marks, you need to do more } 
break;
case 4; case 3;
System.out.println("You have got low marks, see a teacher");
break;
case 2;
System.out.println("You have got very low marks, see a teacher");
break;
case 1;
System.out.println("You only got 1 mark, see a teacher");
break;
default ;
System.out.println("You have failed abysmally") }
}

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Take a look at your syntax. The case statement line should not be terminated with a semicolon.

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

Henry
 
Yong Han
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry
Sry for my mistakes
This is the output after i changed
Can anyone help?

Main.java:3: error: illegal character: '\uff1b'
int marks  =  10?
                ^
Main.java:23: error: unclosed string literal
System.out.println("You got less than half marks, you need to do more } 
                    ^
Main.java:23: error: ';' expected
System.out.println("You got less than half marks, you need to do more } 
                                                                          ^
Main.java:25: error: orphaned case
case 4: case 3:
^
Main.java:35: error: ';' expected
System.out.println("You have failed abysmally") }
                                               ^
5 errors
 
Yong Han
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And this is the question

if (i == 10){
System.out.println("Congratulations you have full marks");
} else if (i == 9) {
System.out.println("Congratulations you have almost full marks");
} else if (i == 8) {
System.out.println("Congratulations you have done very well");
} else if (i == 7) {
System.out.println("Congratulations you have done well");
} else if (i == 6) {
System.out.println("You are doing alright but could study more");
} else if (i == 5) {
System.out.println("You only got half marks, you need to do more work");} System.out.println("You got less than half marks, you need to do more }
else if (i == 3) {
System.out.println("You have got low marks, see a teacher");
} else if (i == 2) {
System.out.println("You have got very low marks, see a teacher");
} else if (i == 1) {
System.out.println("You only got 1 mark, see a teacher");
} else {
System.out.println("You have failed abysmally") }
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yong Han, Welcome to the Ranch!

Please UseCodeTags (that's a link) when you post code.  It look nicer and people are more likely to respond to your post.

What are you using to enter code?  If it is a word processor, this is a poor choice.  Use a notepad-type program instead.

The error is saying that you entered a special character (;) and not (;).  Remove the last character and retype (;).
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

These errors are telling you what is wrong. A string literal ("like this") has opening and closing quotes.  And there are two other characters need at the end of the line.

you should get used to reading errors and deciding how to change your code.  The error messages can sometime be cryptic, but look for the carrat (^).  It's pointing to the place in your code where the error begins.
 
Yong Han
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Knute
Sorry for late reply because  i was busy on something
Thank you for your reply and i already done my coding
I learned a lot and i will keep practicing
Thank you
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!