我正在制作一个猜谜游戏的程序。
问题是我必须将const initialState = [];
const numbersReducer = (state = initialState, action) => {
swicth (action.type) {
case TURN_EVEN:
return state.map(i => (i % 2 === 0 ? i : 0));
// Other action types here...
default:
return state;
}
}
设置为0,以便一个人可以做出的猜测量减少1.但同时循环中设置的条件基于{{1}不是0.意思是一旦它为0,循环终止并继续前进。
我不知道如何解决这个问题,同时让循环中的条件正常工作。
以下是相关循环:
remainingguesses
如果需要,以下是完整的代码:
remainingguesses
答案 0 :(得分:1)
这可能是一种简单的编码方式。
read guesses;
while (guesses > 0) {
read input;
if (input == secret) {
print "you win!!";
return;
}
else {
print "try again!";
}
guesses--;
}
print "Sorry! You are out of guesses";
答案 1 :(得分:1)
简化代码:
#include <stdio.h>
int main()
{
int secretnumber;
int guesses;
int secretnumberguess;
int flag=0;
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 (guesses > 0 && flag==0) {
printf("Player 2: Type your guess and press return (guesses remaining:%d):\n",guesses);
scanf(" %d",&secretnumberguess);
guesses=guesses - 1;
if (secretnumberguess > secretnumber) {
printf("Your guess was greater than the secret number.\n");
}
else if (secretnumberguess < secretnumber){
printf("Your guess was less than the secret number.\n");
}
else{
printf("Your guess was equal to the secret number. You win!\n");
flag=1;
}
}
if (guesses == 0 && flag==0)
printf("Sorry you are out of guesses. You lose.\n");
return 0;
}