gpt4 book ai didi

Java——迭代不起作用

转载 作者:行者123 更新时间:2023-11-30 07:19:09 26 4
gpt4 key购买 nike

我正在开发一款类似于 Cows and Bulls 的 Java 游戏。但不是显示“Cow”和“Bull”,而是显示“MOO”或“moo”。生成一个 4 位数字,用户可以猜出它。对于正确指定的每个数字(相同的数字,相同的位置),屏幕上会显示一个“MOO”。对于猜中的每个具有相同数字但位置错误的数字,都会显示“moo”。我在执行返回多少“moo”的方法时遇到问题。

public int getLittleMooCount(int guess)
{
int count = 0;
String guessString = Integer.toString(guess);
String randomValueString = Integer.toString(randomValue);

// Pads the number with zero if less than 4 digits, so length is always 4
while(guessString.length() < 4)
{
guessString = "0" + guessString;
}

while(randomValueString.length() < 4)
{
randomValueString = "0" + randomValueString;
}

// checking to see if positions EXACTLY match. If so, it would return a MOO (the getBigMoo
// Count is in my other method)
for (int i = 0; i < 4; i++)
{
if (guessString.charAt(i) == randomValueString.charAt(i))
{
randomValueString = randomValueString.replace(randomValueString.charAt(i), 'X');
guessString = guessString.replace(guessString.charAt(i), 'O');
}
}

for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (guessString.charAt(j) == randomValueString.charAt(i))
{
count++;
randomValueString = randomValueString.replace(randomValueString.charAt(i), 'X');
guessString = guessString.replace(guessString.charAt(j), 'O');
}

}
}

return count;
}
  1. 测试输入
    1. 0011
    2. 2233
    3. 4455
    4. 6677
    5. 8899
    6. 1234
    7. 5678
    8. 9090

随机数为 5509 的结果

  1. 1 moo = 正确
  2. 没有=正确
  3. 1 moo = 错误!应该是两个哞哞。
  4. 没有=正确
  5. 1 MOO = 正确
  6. 没有=正确
  7. 1 MOO = 正确
  8. 2 moo = 正确

它似乎对某些数字有效,但对于其他数字,总有一个测试数据是错误的。显然,出了点问题,但我尝试了一切,结果令人沮丧!如果有帮助,我可以发布另一个示例。

提前感谢您的帮助!

编辑:好的,我将其更改为字符数组,现在它什么也没做。我可以发誓我做对了。

int count = 0;
String guessString = Integer.toString(guess);
String randomValueString = Integer.toString(randomValue);

char [] randomValueCharacter = randomValueString.toCharArray();
char [] guessCharacter = guessString.toCharArray();


while(guessString.length() < 4)
{
guessString = "0" + guessString;
}

while(randomValueString.length() < 4)
{
randomValueString = "0" + randomValueString;
}

for (int i = 0; i < 4; i++)
{
if (guessString.charAt(i) == randomValueString.charAt(i))
{
randomValueCharacter[i] = 'X';
guessCharacter[i] = 'O';
}
}

for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (guessString.charAt(j) == randomValueString.charAt(i))
{
count++;
randomValueCharacter[i] = 'X';
guessCharacter[j] = 'O';
}

}
}

return count;
}

最佳答案

是因为String.replace()。此函数不会像您假设的那样替换特定位置的字符,它会替换给定字符的所有外观!所以在找到前 5 个之后,两个 5 都被替换为 x。

Java文档

public String replace(char oldChar, char newChar) Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar. If the character oldChar does not occur in the character sequence represented by this String object, then a reference to this String object is returned. Otherwise, a new String object is created that represents a character sequence identical to the character sequence represented by this String object, except that every occurrence of oldChar is replaced by an occurrence of newChar.

Examples:

"mesquite in your cellar".replace('e', 'o') returns "mosquito in your collar" "the war of baronets".replace('r', 'y') returns "the way of bayonets" "sparring with a purple porpoise".replace('p', 't') returns "starring with a turtle tortoise" "JonL".replace('q', 'x') returns "JonL" (no change) Parameters: oldChar - the old character. newChar - the new character. Returns: a string derived from this string by replacing every occurrence of oldChar with newChar.

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html

您可以改用整数数组。


试试这个

    int count = 0;
String guessString = Integer.toString(4455);
String randomValueString = Integer.toString(5509);

char [] randomValueCharacter = randomValueString.toCharArray();
char [] guessCharacter = guessString.toCharArray();


while(guessString.length() < 4)
{
guessString = "0" + guessString;
}

while(randomValueString.length() < 4)
{
randomValueString = "0" + randomValueString;
}

for (int i = 0; i < 4; i++)
{
if (guessCharacter[i] == randomValueCharacter[i])
{
randomValueCharacter[i] = 'X';
guessCharacter[i] = 'O';
}
}

for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (guessCharacter[j] == randomValueCharacter[i])
{
count++;
randomValueCharacter[i] = 'X';
guessCharacter[j] = 'O';
}

}
}

return count;
}

关于Java——迭代不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14865736/

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