【发布时间】:2019-03-09 07:30:47
【问题描述】:
我试图弄清楚如何确保用户输入正确的字符。基本上,我希望用户输入 c 或 u。到目前为止,它一直有效,直到用户输入以 u 或 c 开头的短语,它仍然可以通过。我希望他们只按 c 或 u 而不在字母上附加任何其他字符。我认为这与数组有关,但我对数组的了解并不多。这里:
#include <stdio.h>
int main()
{
char turn;
printf("Welcome to the game of Sticks. The objective is to pick up the last stick\n\n");
printf("Please choose who goes first. (u for user and c for computer): ");
scanf(" %c", &turn);
while (turn != 'c' && turn != 'u') //Checking if user inputted c or u
{
printf("\nPlease enter u to go first or c for computer to go first!\n");
scanf(" %c", &turn);
}
return 0;
}
【问题讨论】: