Reputation: 41
package edu.blastermind.model;
import java.util.Random;
/**
* A NumberGuessingGame represents the rules of a simple "guess the number" type game.
*
* @author
*
*/
public class GuessTheNumberGame {
private int highest;
private int secret;
/**
* Creates a new NumberGuessingGame with a secret number between 0 and highest.
*
* @param highest the highest possible number for this game. Must be > 0.
*/
public GuessTheNumberGame(int highest) {
// TODO 1: perform a precondition check on the parameter highest
if (highest <= 0) {
throw new IllegalArgumentException
("highest number must be at least 1");
}
this.highest = highest;
Random rng = new Random();
this.secret = rng.nextInt(highest + 1);
}
/**
* Checks to see if we've guessed correctly.
*
* @param guess our guess (must be between 0 and getHighest())
* @return true if the guess was correct, false otherwise
*/
public boolean isGuessCorrect(int guess) {
// TODO 2: perform a precondition check on the variable guess
if (guess > this.highest || guess < 0) {
throw new IllegalArgumentException
("Guess must be between 1 and 100");
}
return this.secret == guess;
}
/**
* Checks to see if our guess is higher than the secret.
*
* @param guess our guess (must be between 0 and getHighest())
* @return true if our guess is higher than the secret, false otherwise
*/
public boolean isGuessHigher(int guess) {
// TODO 3: perform a precondition check on the variable guess
if (guess > this.highest || guess < 0) {
throw new IllegalArgumentException
("Guess must be between 1 and 100");
}
return this.secret < guess;
}
/**
* Returns the highest value in the range of valid guesses.
*
* @return the highest value in the range of valid guesses
*/
public int getHighest() {
return this.highest;
}
}
package edu.blastermind.controllers;
import java.util.Scanner;
import edu.westga.blastermind.model.GuessTheNumberGame;
public class GuessTheNumber {
public static void main(String[] args) {
GuessTheNumberGame game = new GuessTheNumberGame(100);
int turns = 1;
Scanner kb = new Scanner(System.in);
System.out.println("Guess a nummber between 0 and 100");
int guess = kb.nextInt();
// TODO 4: loop as long as the guess is not correct
while (!game.isGuessCorrect(guess)) {
guess++;
if (game.isGuessHigher(guess)){
System.out.println("You guessed too high!");
turns++;
}
else if (!game.isGuessCorrect(guess)){
System.out.println("You guessed too low!");
}
int GuessTheNumber = kb.nextInt();
}
turns++;
// TODO 5: in the loop, check guesses and give hints
System.out.printf("You guessed the number in %d turns\n", turns);
}
}
Hey everyone> I am writing a guess the secret number program in java and I am having some problems. I keep getting that my guess is too high when I run the program the first time and after I terminate the program and run it again it says my guess is too low. In the while loop it is suppose to loop as long as the guess is the wrong number which I believe it is doing but I am obviously doing something wrong. Any help is appreciated, thanks in advance.
Upvotes: 1
Views: 1394
Reputation: 2404
Fixing your main method. You missed a little bit the variables, adding one to guess. And you were always checking on the first input by the user.
public static void main(String[] args) {
GuessTheNumberGame game = new GuessTheNumberGame(100);
int turns = 0;
Scanner kb = new Scanner(System.in);
// TODO 4: loop as long as the guess is not correct
while (true) {
System.out.println("Guess a nummber between 0 and 100");
int guess = kb.nextInt();
if (game.isGuessHigher(guess)) {
System.out.println("You guessed too high!");
} else if (!game.isGuessCorrect(guess)) {
System.out.println("You guessed too low!");
}
turns++;
if (game.isGuessCorrect(guess)) break;
}
// TODO 5: in the loop, check guesses and give hints
System.out.printf("You guessed the number in %d turns\n", turns);
}
Upvotes: 0
Reputation: 691715
Your loop checks the correctness of guess
. And if guess
is incorrect, you read a new value from the command line and assign it to GuessTheNumber
, instead of assigning it to guess
.
I also don't understand why you're doing guess++
. It doesn't make sense.
Upvotes: 2