gpt4 book ai didi

java - 为什么我的代码永远运行?它只是一堆带有应该递减的整数的 for 循环?

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

我编写了一些代码来打印记分牌,但它没有完成运行。记分板应该如下所示

+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|\\ //|\\ //|\\ //|\\ //|\\ //|\\ //|\\ //| | |
| \V/ | \V/ | \V/ | \V/ | \V/ | \V/ | \V/ | | |
| /.\ | /.\ | /.\ | /.\ | /.\ | /.\ | /.\ | | |
|// \\|// \\|// \\|// \\|// \\|// \\|// \\| | |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+

但是当我使用错误猜测的数字运行它时,它会永远运行,而当我使用错误猜测的零运行它时,记分板看起来像这样

+-----+-----+
+-----+-----+
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
+-----+-----+
+-----+-----+

这是我的代码:

public class PhraseGame {

public static String drawScoreboard(int totalGuesses, int incorrectGuesses) {
String result = "";
String finalResult = "";

for (int j = totalGuesses; j > 0; j--) {
for (int g = incorrectGuesses; incorrectGuesses > 0; g--) {
result = ("+-----");
finalResult += result;
}
for (int i = totalGuesses - incorrectGuesses; i > 0; i--) {
result = ("+--a---");
finalResult += result;
}
result = ("+\n");
finalResult += result;
}
for (int j = totalGuesses; j > 0; j--) {
for (int g = incorrectGuesses; incorrectGuesses > 0; g--) {
result = ("|\\\\ //");
finalResult += result;
}
for (int h = totalGuesses - incorrectGuesses; h > 0; h--) {
result = ("| ");
finalResult += result;
}
result = ("|\n");
finalResult += result;
}
for (int j = totalGuesses; j > 0; j--) {
for (int g = incorrectGuesses; incorrectGuesses > 0; g--) {
result = ("| \\V/ ");
finalResult += result;
}
for (int i = totalGuesses - incorrectGuesses; i > 0; i--) {
result = ("| ");
finalResult += result;
}
result = ("|\n");
finalResult += result;
}
for (int j = totalGuesses; j > 0; j--) {
for (int g = incorrectGuesses; incorrectGuesses > 0; g--) {
result = ("| /.\\ ");
finalResult += result;
}
for (int i = totalGuesses - incorrectGuesses; i > 0; i--) {
result = ("| ");
finalResult += result;
}
result = ("|\n");
finalResult += result;
}
for (int j = totalGuesses; j > 0; j--) {
for (int g = incorrectGuesses; incorrectGuesses > 0; g--) {
result = ("|// \\\\");
finalResult += result;
}
for (int i = totalGuesses - incorrectGuesses; i > 0; i--) {
result = ("| ");
finalResult += result;
}
result = ("|\n");
finalResult += result;
}
for (int j = totalGuesses; j > 0; j--) {
for (int g = incorrectGuesses; incorrectGuesses > 0; g--) {
result = ("+-----");
finalResult += result;
}
for (int i = totalGuesses - incorrectGuesses; i > 0; i--) {
result = ("+-----");
finalResult += result;
}
result = ("+");
finalResult += result;
}
result = "\n";
finalResult += result;
return finalResult;
}

public static void main(String[] args) {
System.out.println(PhraseGame.drawScoreboard(4,2));
}


}

我是不是哪里搞错了?

最佳答案

您对总猜测次数进行了两次循环。

第一层循环for (int j =totalGuesses; j > 0; j--) 循环猜测总数一次,而第二层循环in CorrectGuessestotalGuesses - invalidGuesses 再次运行猜测总数。

这意味着总共 3 次猜测,您将打印九次,而不是打印三次!这就是为什么输出的每一行都会被打印多次。

除此之外,正如 @ScaryWombat 的答案所示,您的 in CorrectGuesses 循环是不正确的,因为它比较 in CorrectGuesses > 0 而不是比较 g > 0.

关于java - 为什么我的代码永远运行?它只是一堆带有应该递减的整数的 for 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58907821/

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