Find the Error Assume the following declaration exists enum
Solution
What\'s wrong about it?
Does it not compile?
Does it give an unexpected result?
the line Coffee myCup = DARK needs to become Coffee myCup = Coffee.DARK
as because DARK is part of Coffee so we must use Coffee.DARK.
Second error is in the switch case.
we must have to declare as name itself
switch (myCup)
{
case MEDIUM :
System.out.println(\"Mild flavor.\");
break;
case DARK :
System.out.println(\"Strong flavor.\");
break;
case DECAF :
System.out.println(\"Won\'t keep you awake.\");
break;
default:
System.out.println(\"Never heard of it.\");
}
myCup is be defined as dateType of Coffee so no need of decalring case options as Coffee.\"Names\"
Here is corrected code:
public class HelloWorld{
enum Coffee { MEDIUM, DARK, DECAF };
public static void main(String []args){
Coffee myCup = Coffee.DARK;
switch (myCup)
{
case MEDIUM :
System.out.println(\"Mild flavor.\");
break;
case DARK :
System.out.println(\"Strong flavor.\");
break;
case DECAF :
System.out.println(\"Won\'t keep you awake.\");
break;
default:
System.out.println(\"Never heard of it.\");
}
}
}
Output:
Strong flavor.

