my dog learned polymorphism
The moose likes Beginning Java and the fly likes Whats the output... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Whats the output..." Watch "Whats the output..." New topic

Whats the output...

Mohnish Khiani
Ranch Hand

Joined: May 17, 2010
Posts: 65

The Output given is 21... I couldn't understand how...please explain..
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 235

Do you run it using IDE like Eclipse?
You could check how the code executes using debug mode.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20136

The main consideration is the lack of break statements. That means that if roman == Roman.X three switch statements will be executed:
- roman = Roman.C;
- if(roman.ordinal>2)z+=5;
- x++

Now just loop through the code; I suggest you do it manually. Like this:
- start: x == 7, z == 2, roman == X
- iteration 1: roman == X so roman = C, z += 5, x++, and another z++. At the end, x == 8, z == 8 and roman == C
- iteration 2: ...

Renamed your Roman variable because the forum won't let me use its name outside code blocks.

How To Ask Questions How To Answer Questions
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

And the code as it is, can't compile...

HINT: Error is in line 14 (maybe just a small typo)

Regards, Rene Larsen
Dropbox Invite
Mohnish Khiani
Ranch Hand

Joined: May 17, 2010
Posts: 65
sorry for that...that was my typing error...
Mohnish Khiani
Ranch Hand

Joined: May 17, 2010
Posts: 65
hey...sorry and thanks for your replys i got it...i was making a silly mistake...thanks everyone
I agree. Here's the link:
subject: Whats the output...
It's not a secret anymore!