on 09-27-2006 12:54 PM
hello ! i m a beginner in java. Kindly help me to solve this question.
Watll 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"
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
91 | |
10 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.