Forums Register Login

enum doubt

+Pie Number of slices to send: Send
Hi this is one of the question i found in mock exam

class Stepper{
enum Roman {I,V,X,L,C,M}
public static void main(String... bang){
int x=7;
int z=2;

Roman r=Roman.X;
do{
switch(r){
case C:r=Roman.L;break;
case X:r=Roman.C;
case L:if(r.ordinal()>2)z+=5;
case M:x++;
}
z++;
}while(x<10);
System.out.println(z);
}
}
---------------------------
output:21
---------------------------
i have just one statement which i did not understand

case L:if(r.ordinal()>2)z+=5;
please can you explain me what does above statement mean,i really don't
understand what ordinal is.

THANK YOU
+Pie Number of slices to send: Send
From the Api (http://java.sun.com/j2se/1.5.0/docs/api/) Enum page


ordinal()
Returns the ordinal of this enumeration constant (its position in its enum declaration, where the initial constant is assigned an ordinal of zero).


So the behaviour depends on the ordinal I = 0, V = 1, X = 2 and so on, so L,C and M > 2.

What I'm not sure is the purpose of the code.
Why does your bag say "bombs"? The reason I ask is that my bag says "tiny ads" and it has stuff like this:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 873 times.
Similar Threads
Problem with a mock exam question
Whats the output...
Enum Question
mock question
please explain - code output
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 08:46:37.