• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Whats the output...

 
Mohnish Khiani
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The Output given is 21... I couldn't understand how...please explain..
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you run it using IDE like Eclipse?
You could check how the code executes using debug mode.
 
Rob Spoor
Sheriff
Pie
Posts: 20493
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the code as it is, can't compile...

HINT: Error is in line 14 (maybe just a small typo)
 
Mohnish Khiani
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry for that...that was my typing error...
 
Mohnish Khiani
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey...sorry and thanks for your replys i got it...i was making a silly mistake...thanks everyone
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic