繁体   English   中英

Java:猜数游戏

[英]Java: Number Guessing Game

我的班级代码: http : //notepad.cc/lureascu84我的测试员代码: http : //notepad.cc/mammivo62

我被困在如何在类程序中导入代码以在测试仪中使用。 在我的简单数字猜测程序中,我有:

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!");

当使用访问器和修改器方法时,我被困在写什么上。

我相信你遇到的问题是实例化GuessingGame(int maxValue)类。 您需要创建一个将调用类的构造函数的新对象,然后您将能够执行其中包含的方法。

GuessingGame game = new GuessingGame(10);

以上从您的测试类创建了一个新的 GuessingGame 对象,类似于创建一个Random random = new Random() ,其中“maxValue”已设置为 10。从这里您可以进行方法调用。

game.guess(9); 

将运行guess(int newGuess)并可能输出用户的猜测太低。

这就是我解决这个问题的方法:

//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);
        }


}

}

这是我为随机数猜测游戏制作的程序。 虽然代码可能看起来很长,但它非常简单。 特征:-

(1). 1 - 10 之间生成一个数字 玩家可以通过...重播游戏... (2). 在游戏结束时按“T”或按“E”退出游戏... (3 ). 如果玩家输入任何错误的值,程序不会得到... (4). 终止。 玩家可以看到他们进行的尝试次数...希望有帮助!!!

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');
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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