作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个猜谜游戏的程序。然而,用于识别个人是否猜测正确数字的循环甚至无法开始我得到这个:
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/
我是一名优秀的程序员,十分优秀!