gpt4 book ai didi

c - 结果,While 循环无法开始忽略 if 语句和条件

转载 作者:行者123 更新时间:2023-11-30 16:47:41 26 4
gpt4 key购买 nike

我正在开发一个猜谜游戏的程序。然而,用于识别个人是否猜测正确数字的循环甚至无法开始我得到这个:

Player 1: Type a number between 0 and 99 and press return:
1
Type the number of guesses that player 2 gets and press return:
1
Sorry you are out of guesses. You lose.

循环在开始之前就终止了,我不知道如何让它工作。

这是循环的代码:

  printf( "Type the number of guesses that player 2 gets and press return: \n");
scanf("%d",&guesses);

while (remainingguesses != 0) {
printf("Player 2: Type your guess and press return (guesses remaining:%d):\n",remainingguesses);
scanf(" %d",&secretnumberguess);

if (secretnumberguess > secretnumber) {
printf("Your guess was greater than the secret number.\n");
remainingguesses = guesses - 1;
}

else if (secretnumberguess < secretnumber){
printf("Your guess was less than the secret number.\n");
remainingguesses = guesses - 1;
}

else{
printf("Your guess was equal to the secret number. You win!\n");
}
}

if (remainingguesses == 0)
printf("Sorry you are out of guesses. You lose.\n");

我对编程还很陌生,请原谅我的无知。

如果您想查看完整的代码:

#include <stdio.h>

int main()
{

int secretnumber;
int guesses;
int secretnumberguess;
int remainingguesses;

while (1) {
printf("Player 1: Type a number between 0 and 99 and press return:\n");
scanf(" %d",&secretnumber);

if (secretnumber > 99 || secretnumber < 0) {
printf("Secret number cannot be greater than 99 or below 0.\n");
continue;
}
break;
}

printf( "Type the number of guesses that player 2 gets and press return: \n");
scanf("%d",&guesses);

while (remainingguesses != 0) {
printf("Player 2: Type your guess and press return (guesses remaining:%d):\n",remainingguesses);
scanf(" %d",&secretnumberguess);

if (secretnumberguess > secretnumber) {
printf("Your guess was greater than the secret number.\n");
remainingguesses = guesses - 1;
}

else if (secretnumberguess < secretnumber){
printf("Your guess was less than the secret number.\n");
remainingguesses = guesses - 1;
}

else{
printf("Your guess was equal to the secret number. You win!\n");
}
}

if (remainingguesses == 0)
printf("Sorry you are out of guesses. You lose.\n");

return 0;
}

最佳答案

你的剩余猜测导致你无法进入循环。也许不使用它作为检查,而是使用标志并在循环内部检查 if 循环中的剩余猜测。并在 if 循环内相应地更改标志。

关于c - 结果,While 循环无法开始忽略 if 语句和条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43244415/

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