12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- import java.util.Scanner;
- /**
- * Write a description of class Guess here.
- *
- * @author (your name)
- * @version (a version number or a date)
- */
- public class Game
-
- {
- int count;
- int randomNumber;
- int prevGuess;
-
- /**
- * Start game.
- */
- public Game()
- {
- }
-
- /**
- *
- */
- public boolean checkGuess(int usersGuess)
- {
- boolean continuePlay = true;
-
- if ((usersGuess == prevGuess)||(usersGuess < 1)||(usersGuess > 100)){
- System.out.println( "Try again!");
- //for when the user guesses number outside of range or previous guess
- } else if (usersGuess > randomNumber)
- {
- count++;
- prevGuess = usersGuess;
- System.out.println ("Too large! Guess again.");
-
- } else if (usersGuess < randomNumber)
- {
- count++;
- prevGuess = usersGuess;
- System.out.println ("Too small! Guess again.");
-
- }else{
- count++;
- prevGuess = usersGuess;
- System.out.println ("Correct! " + count + " guesses");
- continuePlay = false; //stops the while loop
- }
- return continuePlay;
- }
-
- public void play(){
- boolean continuePlay = true;
- //reset counter, random number, and previous guess each time game is played
- count = 0;
- randomNumber = (int)(Math.random() * 100);
- prevGuess = 0;
- while(continuePlay){
- Scanner scanner = new Scanner(System.in);
- System.out.println ("Enter guess between 1 and 100.");
- int guess = scanner.nextInt();
- continuePlay = checkGuess(guess); //true or false is returned from the checkGuess method to continue or stop loop
- }
- }
- }
|