Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

guessing game

 
nathan gibson
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


i am trying to make a program that will create a secret number, once it has been randomly selected the program runs a loop until guessing random numbers until it finds the secret number. but, my program simply creates a random number to try to guess the secret number only once and continually loops that one number down the screen. im not sure what is wrong, but if someone knows, please help.
 
nathan gibson
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i looked through the apis and found a better way to create my random number, but it still doesnt work properly.

 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no statement that would create a new random number in the loop, that's why it loops endlessly.

I'm not sure why you're checking for "<"; from your description it sounds as if you should be checking for "!=" - in which case the the second while statement is superfluous, because both numbers are guaranteed to be equal at that point.
 
nathan gibson
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so would this work any better? im using blue j and when i compile i dont get any errors, but it wont run.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The secret number should only be calculated once, shouldn't it? And that should happen before the loop starts.

As it is, both numbers are zero initially, so the loop is never executed because it checks for inequality.
 
Henry Wong
author
Marshal
Pie
Posts: 21512
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, if the guess is wrong, don't you want to print out what was the guess, before you generate another guess?

Henry
 
nathan gibson
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i figured it out. thank you guys for the help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic