gpt4 book ai didi

java - 嵌套循环在 Mastermind 游戏中无法正常工作

转载 作者:行者123 更新时间:2023-12-01 09:52:07 25 4
gpt4 key购买 nike

我最近开始使用 Java 编码,我的教练给了我一个练习,要求我重新创建 Mastermind 游戏。概述一下这个游戏:计算机创建一个包含 X 个随机整数的数组,用户也可以输入 X 个整数。位置很重要。如果用户猜出与计算机生成的数组位于同一位置的整数,则用户将获得“金牌”。如果该整数存在于数组中,但位置错误,则用户将获得“银”分。如果数组中根本不存在该整数,则用户将获得“NotFound”分数。最终的数组应该为用户提供数组中每个位置的分数,例如(金奖、银奖、未找到)

我尝试制作一个嵌套循环来对用户的猜测进行评分。它捕获不同数组中的分数 (yourScore[])。用户猜测被捕获在数组“guessednums[]”中,计算机生成的数组被称为“nums[]”。所有数组的大小已在上述代码之前使用变量设置。

我想要代码做的是首先检查用户的猜测是否与计算机在同一位置的选择匹配,如果是这种情况,则将 yourScore 数组中的匹配空间设置为“Gold”。然后,如果猜测不直接匹配,我想要一个循环来检查用户猜测是否存在于计算机生成的 nums[] 数组的任何位置,如果是这种情况,则将其评分为“银”。最后,如果不是这种情况,我希望将 yourScore[] 位置设置为“NotFound”。

匹配的猜测正确地得分为“金牌”。我遇到的问题是,有时循环不会正确地将猜测得分为“Silver”,而是将其标记为“NotFound”。我怀疑这与循环未正确初始化有关。我已经阅读了 Stack Overflow 上的多个问题和其他文章,并尝试了我的代码,但我不断遇到类似的问题。

我很想获得关于代码的第二意见,看看我做错了什么。

package Mastermind;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class Mastermind {
static Scanner userInput = new Scanner(System.in);

public static void main(String[] args){

int size = 3; // Allows you to set the size of the arrays in the game

int[] nums = generateNumbers(size); //Stores the computer choice 3-tuple
int[] guessednums; //Stores the guessed 3-tuple
int iteration = 0; //Keeps track of the # of guesses so far
boolean correctAnswer; //true if the guessed tuple matches the computer choice tuple

//Set array size


//The game starts here
while (true){
iteration++;
System.out.println("Iteration #" + iteration);
guessednums = guessTheNumbers(size);
correctAnswer = yourScore(nums, guessednums, size);
if(correctAnswer) break;
}

//Printing the result
printResults(iteration, nums);

}

private static void printResults(int iteration, int[] nums) {
System.out.println("************************************************************"); // Print final result if users has a completely matching guess
System.out.println("************************************************************");
System.out.println("The correct answer was " + Arrays.toString(nums));
System.out.println("It took you " + iteration + " iterations to find the answer!");
}

private static int[] guessTheNumbers(int size) {
System.out.println("Please your ordered guess (press enter after each):");
int[] guessednums = new int[size]; // Initialise array for user choices

for (int i = 0; i < size; i++){ // Loop that creates the array of user input
guessednums[i] = userInput.nextInt();
}

System.out.println(Arrays.toString(guessednums));
return guessednums; // Return array for user guessed numbers array to main method
}

public static int[] generateNumbers(int size){
int[] nums = new int[size]; // Initialise array for computer choices
Random rn = new Random(); // Create new variable for randomised computer choices

for (int i = 0; i < size; i++){ // Loop that creates the array of computer choices
nums[i] = rn.nextInt(9) + 1;
}

System.out.println(Arrays.toString(nums)); // Temporary to print array
return nums; // Return array for computer generated numbers array to main method

}

public static boolean yourScore(int[] nums, int[] guessednums, int size){
String[] yourScore = new String[size]; // Initialise array for user choices

for(int i = 0; i < size; i++){ // Nested loop that scores the user entries as Gold/Silver/NotFound
if (guessednums[i] == nums[i]){
yourScore[i] = "Gold";
} else {
yourScore[i] = "NotFound";// in case is not found it stays that way
for(int j = 0; j < size; j++){
if (guessednums[i] == nums[j]){
yourScore[i] = "Silver"; // found one! break the loop and keep checking numbers
break;
}
}
}
}


if (yourScore[0] == "Gold" && yourScore[1] == "Gold" && yourScore[2] == "Gold"){ // Marks the input as true or false depending on if it matches with the computer choices
boolean correctanswer = true;
return correctanswer;
} else {
System.out.println("Your score is " + Arrays.toString(yourScore) + "!");
System.out.println("************************************************************");
boolean correctanswer = false;
return correctanswer;
}
}
}

最佳答案

问题出在逻辑上。我发现将逻辑转化为文字很有用:

对于数组中的每个数字:1. 检查该位置的数字是否相等(金)2 否则,对于数组中的每个数字,检查该数字是否等于数组中的另一个数字3. 如果该数字在其中,则打破循环(银)4. 否则,将其标记为未找到(NotFound)。

代码如下:

for(int i = 0; i < size;  i++){ // Nested loop that scores the user entries as Gold/Silver/NotFound
if (guessednums[i] == nums[i]){
yourScore[i] = "Gold";
} else {
yourScore[i] = "NotFound";// in case is not found it stays that way
for(int j=0;j<size<j++){
if (guessednums[i] == nums[j]){
yourScore[i] = "Silver"; // found one! break the loop and keep checking numbers
break;
}
}
}
}

关于java - 嵌套循环在 Mastermind 游戏中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37542273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com