还未曾见过倒悬山、、 2023-02-17 13:18 采纳率: 0%
浏览 30

猜32位纯数字Java

2.猜数字规则:系统生成随机选32位数字(例如859632587459632587412589658558745),参加游戏的人员同时猜测当前随机串对应位数上的数值。若某一人员猜测位数和对应位数的数字匹配,则表示此人此次此位猜测正确,此时平台随机数会重新生成,所有人重新猜新生成的随机数,各选手已经猜对的位次无需重新猜。若参加游戏的人员将1-32位所有位次都猜测正确过,此局游戏结束,此选手为获胜选手。

  • 写回答

2条回答 默认 最新

  • CodeBytes 2023-02-17 13:46
    关注

    该回答引用ChatGPT

    import java.util.*;
    
    public class GuessNumberGame {
        private static final int NUMBER_LENGTH = 32;
        private static final int MAX_TRIES = 100;
        private static final int MAX_PLAYERS = 10;
        private static final Random random = new Random();
    
        private static String generateNumber() {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < NUMBER_LENGTH; i++) {
                sb.append(random.nextInt(10));
            }
            return sb.toString();
        }
    
        public static void main(String[] args) {
            // Generate the random number
            String numberToGuess = generateNumber();
            System.out.println("Number to guess: " + numberToGuess);
    
            // Initialize the game state
            boolean[] guessed = new boolean[NUMBER_LENGTH];
            int[] numTries = new int[MAX_PLAYERS];
            String[] playerNames = new String[MAX_PLAYERS];
            int numPlayers = 0;
    
            // Get player names
            Scanner scanner = new Scanner(System.in);
            while (numPlayers < MAX_PLAYERS) {
                System.out.print("Enter player name (or 'quit' to start the game): ");
                String name = scanner.nextLine();
                if (name.equalsIgnoreCase("quit")) {
                    break;
                }
                playerNames[numPlayers++] = name;
            }
    
            // Start the game
            int numGuessed = 0;
            int numPlayersRemaining = numPlayers;
            while (numGuessed < NUMBER_LENGTH && numPlayersRemaining > 0) {
                // Get the next guess from each player
                for (int i = 0; i < numPlayers; i++) {
                    if (numTries[i] < MAX_TRIES) {
                        System.out.print(playerNames[i] + ", enter your guess (32 digits): ");
                        String guess = scanner.nextLine();
                        if (guess.length() != NUMBER_LENGTH) {
                            System.out.println("Your guess must have 32 digits!");
                            continue;
                        }
                        // Check the guess
                        for (int j = 0; j < NUMBER_LENGTH; j++) {
                            if (guess.charAt(j) == numberToGuess.charAt(j) && !guessed[j]) {
                                guessed[j] = true;
                                numGuessed++;
                                numTries[i]++;
                            }
                        }
                        // Check if the player has won
                        if (numGuessed == NUMBER_LENGTH) {
                            System.out.println(playerNames[i] + " wins!");
                            return;
                        }
                        // Check if the player has used up all their tries
                        if (numTries[i] == MAX_TRIES) {
                            System.out.println(playerNames[i] + " is out!");
                            numPlayersRemaining--;
                        }
                    }
                }
            }
            // Game over
            System.out.println("Nobody wins!");
        }
    }
    
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 2月17日

悬赏问题

  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库