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!"); } }
解决 无用评论 打赏 举报
悬赏问题
- ¥15 在虚拟机中安装flash code
- ¥15 单片机stm32f10x编写光敏电阻调节3.3伏大功率灯亮度(光强越大灯越暗,白天正常光强灯不亮,使用ADC,PWM等模块)望各位找一下错误或者提供一个可实现功能的代码
- ¥20 verilog状态机方法流水灯
- ¥15 pandas代码实现不了意图
- ¥15 GD32H7 从存储器到外设SPI传输数据无法重复启用DMA
- ¥25 LT码在高斯信道下的误码率仿真
- ¥45 渲染完成之后将物体的材质贴图改变,自动化进行这个操作
- ¥15 yolov5目标检测并显示目标出现的时间或视频帧
- ¥15 电视版的优酷可以设置电影连续播放吗?
- ¥50 复现论文;matlab代码编写