• Post Reply Bookmark Topic Watch Topic
  • New Topic

My solution of Rosetta Codes 100 Doors puzzle does absolutely nothing!  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, I'm about three quarters of the way through Head First Java and I decided to try my hand at some Rosetta Code puzzles for entertainment so I'm capable in Java but not the most experienced at the moment.

My solution for the 100 doors puzzle is at the bottom. It prints out false for all 100 doors when it's supposed to print true for every squared number (Door 1, 4, 9 ,16, 36, 49, 64, 81 and 100). This is a PITA and I'm hoping someone can figure what I've done wrong here.

 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hint: take a look at the "z" variable. You will see that it doesn't really do what you are expecting.

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Using y++ means you're looking at every door. Then you have that i and z stuff that seems like you're trying to skip the appropriate doors, but it's apparently not doing what you think it is.

Much simpler just to change this loop increment piece to go to the correct door. However, before you do, as an educational exercise in learning how to debug, I suggest you print out your variables, door number, what you're doing to it, etc. I'd suggest starting with 5-10 loops through 5-10 doors for that.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!