用 Java 创建一个猜数游戏

Zeeshan Afridi 2022年5月31日
用 Java 创建一个猜数游戏

猜谜游戏对用户来说非常简单;它只是在 k 次试验中猜测正确的数字来赢得比赛。但对于开发者而言,事情就没有那么简单了。

用 Java 创建一个猜数游戏

作为开发人员,你需要牢记这款游戏的约束和算法。

猜谜游戏规则

你必须牢记规则才能开发猜谜游戏。为了赢得游戏,用户必须满足游戏的定义约束。

  1. 数量范围可以根据你的喜好而有所不同;但是,对于这个程序,范围是从 1 到 100。

  2. 如果猜中的数字大于用户输入的数字,程序将回复消息:哎呀!你的猜测太高了,请尝试一些较低的数字

  3. 如果猜中的数字小于用户输入的数字,程序将回复消息:Opss!你的猜测太低了,请尝试一些更高的数字

  4. 如果用户输入的数字与实际猜中的数字相同,则用户中奖。将显示的消息是完美!你答对了!

    将要求用户再次播放。他可以选择终止比赛或重新比赛。

  5. 如果达到了尝试次数的限制,并且用户没有猜到次数,游戏将结束并显示消息:

    The number was 64
    Better luck next time
    Want to Play again?(y/n)
    

    用户可以再次玩游戏或退出游戏。

在 Java 中创建猜谜游戏的算法

该算法是逐步解决问题的方法。猜谜游戏的算法非常简单。

让我们看看用 Java 开发猜谜游戏的步骤。

  • 从程序中取出一个随机数并将其存储在整数数据类型的变量中。
  • 现在,从用户那里获取输入。输入数据类型必须与实际随机数相同。
  • 下一步是比较用户的号码和实际的随机数。
  • 如果用户猜测的数字大于实际随机数,则显示消息 Higher value
  • 如果用户猜测的数字小于实际随机数,则显示消息 Lower value
  • 如果用户猜测的数字等于实际的随机数。用户赢得了比赛;要求再玩。
  • 如果达到了轨迹数,而用户还没有猜到数字,则显示实际数字并要求再次播放。

使用 Random() 方法在 Java 中生成随机数

在 Java 中生成随机数非常容易。Java 专门为随机数 java.util.Random 提供了一个完整的库,你可以使用 import 关键字轻松导入它。

在 Java 中导入随机数的正确语法是 import java.util.Random;

示例代码:

package articlecodesinjava;
import java.util.Random;
 public class GuessingGame {

    public static void main(String[] args) {

        int x;
        Random generator = new Random();
        System.out.println("5 random numbers from 0 to 10");
        for(int i = 0; i < 5; i++){
            x = generator.nextInt(5);
            System.out.print(x+" , ");
        }
    }
}

输出:

5 random numbers from 0 to 10
3 , 1 , 3 , 4 , 4 ,

Java 猜谜游戏的示例代码

package articlecodesinjava;
import java.util.Random;
import java.util.Scanner;

public class GuessingGame {

    public static void main(String[] args) {

        int ans, guess, guessNum = 0;
        final int maxGuesses = 5;
        String str, playAgain = "y";

        Scanner scan = new Scanner(System.in);
        Random generator = new Random();
        ans = generator.nextInt(101) + 1;

        while (playAgain.equals("y") || playAgain.equals("Y")) {
            System.out.println("Hey Welcome to the game \nGuess a number between 1 and 100");
            System.out.println("Guess a number (0 to quit):");

            guess = scan.nextInt();
            guessNum = 0;
            while (guess != 0)
            {
                guessNum++;
                if (guess == ans) {
                    System.out.println("Perfect! You got it Right!");
                    break;
                } else if (guess < ans)
                    System.out.println("Opss! Your guess was too LOW, try some higher numbers.");
                else if (guess > ans)
                    System.out.println("Opps! Your guess was too HIGH, Try some lower numbers");
                if (guessNum == maxGuesses) {
                    System.out.println("The number was " + ans +"\nBetter luck next time");
                    break;
                }
                System.out.println("Enter your guess (0 to quit):");
                guess = scan.nextInt();
            }
            System.out.println("Want to Play again?(y/n)");
            playAgain = scan.next();
        }
        System.out.println("Thanks for playing!");
    }
}

输出:

Hey Welcome to the game
Guess a number between 1 and 100
Guess a number (0 to quit):
1
Opss! Your guess was too LOW, try some higher numbers.
Enter your guess (0 to quit):
2
Opss! Your guess was too LOW, try some higher numbers.
Enter your guess (0 to quit):
55
Opss! Your guess was too LOW, try some higher numbers.
Enter your guess (0 to quit):
76
Opps! Your guess was too HIGH, Try some lower numbers
Enter your guess (0 to quit):
78
Opps! Your guess was too HIGH, Try some lower numbers
The number was 64
Better luck next time
Want to Play again?(y/n)
Zeeshan Afridi avatar Zeeshan Afridi avatar

Zeeshan is a detail oriented software engineer that helps companies and individuals make their lives and easier with software solutions.

LinkedIn