cancel
Showing results for 
Search instead for 
Did you mean: 

switch case

Former Member
0 Kudos

hello ! i m a beginner in java. Kindly help me to solve this question.

Wat’ll happen when u compile and run this code

public class MySwitch{

public static void main(String argv[]){

MySwitch ms=new MySwitch();

ms.amethod();

}

public void amethod(){

int k=10;

switch(k){

default: // do nothing

System.out.println("this is the default output");

break;

case 10; System.out.println("ten");

case 20: System.out.println("twenty");break;

}

}

}

Options:

1. none of these options

2. Compile time error target of switch must be an integral type

3.Compile and run with output " thid is the default output"

4.Compile and run with output of the single line "ten"

Accepted Solutions (1)

Accepted Solutions (1)

suresh_krishnamoorthy
Active Contributor
0 Kudos

Hi Payal,

You code will give following error:

java.lang.Error: Unresolved compilation problem:

Syntax error on token ";", ":" expected.

because case 10; should be changed to case 10: then it will give output:

ten

twenty

you have to put break; between case 10 and case 20 then output will be: ten

You can also test these small program in NWDS.

see this link also:

http://www.javacoffeebreak.com/books/extracts/javanotesv3/c3/s6.html

Regards, Suresh KB

Answers (0)