简体   繁体   中英

Java: Number Guessing Game

My class code: http://notepad.cc/lureascu84 My tester code: http://notepad.cc/mammivo62

I am stuck on how to import the code in the class program to use in the tester. On my simple number guess program I have:

while (guess != num){
     guess = scanner.nextInt();

     if (guess > num){
        System.out.println("The number you have entered is too high!");
        numberOfGuesses++;
     }

     if (guess < num){
        System.out.println("The number you have entered is too low!");
        numberOfGuesses++;
     }
  }
  System.out.println("You win with only " + numberOfGuesses + " wrong attempts!");

When using an accessor and mutator method I'm stuck on what to write.

I believe what youère getting stuck on is instantiating the GuessingGame(int maxValue) class. You need to create a new object that will call the constructor of the class, then you will be able to execute the methods contained within.

GuessingGame game = new GuessingGame(10);

The above creates a new GuessingGame object from your tester class, similar to creating a Random random = new Random() , where "maxValue" has been set to be 10. From here you can make method calls.

game.guess(9); 

Would run the guess(int newGuess) and presumably output that the user's guess was too low.

this is how i would solve that problem:

//creates a public function
public static void main(String[] args) {
    //states that r will be the new random number
    Random r = new Random();
    //sets 1 as the lowest possible number
    int Low = 1;
    //sets 10 as the highest number possible
    int High = 10;
    //generates the random number that is in between or is one of the biggest/lowest possible numbers 
    int Result = r.nextInt(High-Low) + Low;

    //states the variable that will keep count of number of tries by the user.
    int numOfTries = 0;

    //prints out intro to the game
    System.out.println("Hello! lets play a game!");
    System.out.println("Guess a number between 1 and 10");
    System.out.println(" ");
    System.out.print("What is your guess: ");
    System.out.println(" ");

    //states the scan variable
    Scanner scan = new Scanner(System.in);

    //loop function that will repeat till satisfied 
    for ( numOfTries=1; numOfTries<6;numOfTries++ ) {   
        // allows the user to enter their guess
            int number = scan.nextInt();
        if (number == Result) { //this is what happens when the user guesses right within 5 tries
            System.out.println("Correct! It took you " + numOfTries + " to guess the right number!");
            System.out.println("Game over, YOU WON!");
            break;
        }
        else if (number < Low || number > High) { //reminds the user of the guessing range
            System.out.println("Remember that the guessing range is between " + Low + " though " + High);
            System.out.println("Guess again...");
        }
        else if (number < Result && numOfTries < 5) { //tells the user if the guess it too small
            System.out.println("Your guess is too small. Guess again.");

        }
        else if (number > Result && numOfTries < 5) { //tells the user is the guess is too large
            System.out.println("Your guess is too big. Guess again.");

        }
        else if (number > Result && numOfTries == 5) { //tells user game over if didn't guess right after 5 tries
            System.out.println("Your guess is too big. GAME OVER, LOSER!");
            System.out.println("You were not able to guess the right number in 5 tries.");
            System.out.println("The correct number is " + Result);
        }
        else if (number < Result && numOfTries == 5) { //tells the user game over if didn't guess right after 5 tries 
            System.out.println("Your guess is too small. GAME OVER, LOSER!");
            System.out.println("You were not able to guess the right number in 5 tries.");
            System.out.println("The correct number was " + Result);
        }


}

}

This is the program that I made for the Random Number Guess game. Although the code may look long, it's very simple. Features:-

(1).A number is generated between 1 - 10 The player can replay the game by... (2).pressing 'T' or exit the game by pressing 'E' at the end of the game... (3).If the player inputs any wrong value the program doesn't get... (4).terminated. The player can see the number of tries made by them...HOPE IT HELPS !!!

package FunProjects;
import java.util.*;
public class RandomGuessGame {
    public static void main(String[] args) {
        int num;        //the guess number input by the user
        double ran_num; //Random number generated by the game
        char again;     //press 't' or 'e' to try again or to exit the game
        int i      //variable which is being incremented for the variable 'tries'
        Scanner scs = new Scanner(System.in);
        do {
            i = 1;
            {

                System.out.println("Random number between 0 - 10 has been generated");
                Random rand = new Random();
                ran_num = rand.nextInt(10);
                do {
                        int tries = i++; // number of tries by the user
                        System.out.println("Enter Your Guess");
                        Scanner sc = new Scanner(System.in);
                        num = sc.nextInt();
                        if (num == ran_num)
                        {
                            System.out.println("*********************************");
                            System.out.println("*********************************");
                            System.out.println("Congratulations!!! Correct Guess   :)  ");
                            System.out.println("Your Guess -->  " + num);
                            System.out.println("Number of tries = " + tries);
                            System.out.println("*********************************");
                            System.out.println("*********************************");
                        } else if (num < ran_num)
                        {
                            System.out.println("Wrong !!!   :(");
                            System.out.println("Go Higher");
                        } else if (num > ran_num)
                        {
                            System.out.println("Wrong !!!   :(");
                            System.out.println("Go Lower");
                        }
                        else if (num > 10 || num < 0)
                        {
                            System.out.println("Your guess exceed the limit ");
                            System.out.println("Enter your guess Between 1 - 10");
                        }
                        else
                            {
                                System.out.println("Undefined Output   -_-");
                            }
                }   while (num != ran_num);
                do
                    {

                        System.out.println("Press 'T' to try again");
                        System.out.println("Press 'E' to exit the game");
                        again = scs.next().charAt(0);
                        {
                            if (again != 't' && again != 'T' && again != 'e' && again != 'E')
                            {
                                System.out.println("Wrong Input ");
                            }
                        }
                    }
                while (again != 't' && again != 'T' && again != 'e' && again != 'E');
                if (again == 'e' || again == 'E')
                {
                    System.exit(0);
                }
            }
        }
            while (again == 't' || again == 'T');
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM