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

GuessingGame

 
kofi ofei
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
could i get help in getting this program to work. there are two errors. thanks


public class GuessingGame {

public static void main(String[] args) {
System.out.println("Let's play a game. I'll pick a number between");
System.out.println("1 and 100, and you try to guess it.");
boolean playAgain;
do {
playGame(); // call subroutine to play one game
System.out.println("Would you like to play again? ");
playAgain = getlnBoolean();
} while (playAgain);
System.out.println("Thanks for playing. Goodbye.");
} // end of main()

static void playGame() {
int computerNumber; // A random number picked by the computer.
int usersGuess; // A number entered by user as a guess.
int guessCount; // Number of guesses the user has made.
computerNumber = (int)(100 * Math.random()) + 1;
// The value assigned to computerNumber is randomly
// chosen integer between 1 and 100, inclusive.
guessCount = 0;
System.out.println();
System.out.println("What is your first guess?");
while (true) {
usersGuess = getInt(); // get the user's guess
guessCount++;
if (usersGuess == computerNumber) {
System.out.println ("You got it in" + guessCount
+ " guesses! My number was " + computerNumber);
break; // the game is over; the user has won
}
if (guessCount == 6) {
System.out.println("You didn't get the number in 6 guesses.");
System.out.println("You lose. My number was " + computerNumber);
break; // the game is over the user has lost
}
// If we get to this point, the game continues.
// Tell the user if the guess was too high or too low.
if (usersGuess < computerNumber)
System.out.println("That's too low. Try again: ");
else if (usersGuess > computerNumber)
System.out.println("That's too high. Try again: ");
}
System.out.println ();
} // end of playGame ()

} // end of class GuessingGame
 
Ransika deSilva
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Well I guess you have posted this question in the wrong forum, and it is so hard to read the code like this, please use the UBB Code when you are pasting the java code. Further more, please do tell us what are the errors, it is much easier for us then to answer. Regards.
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i posted mine at beginner´┐Żs
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the Java In General (Beginner) forum.

Please try to post your questions n the most appropriate forum. Since this forum is about J2ME and you have code that has public static void main. Means that it is not a question about J2ME.

Mark
 
Nigel Browne
Ranch Hand
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quite simple really. You are not reading the user input. You have two methods getlnBoolean() and getInt() which are meant to read the user input but are not defined anywhere.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic