#define _CRT_SECURE_NO_WARNINGS 1#include stdioh#includectypehint mainvoid char another_game = Y; printfn扮演简单西蒙; printf注意屏幕上的数字序列; printfn小心观察因为数字只显示一秒钟 !; printfn编译器会重新编译它们然后提示您; printf输入相同的序列; prin
在使用scanf函数接收用户输入时,需要注意以下问题:
- 在输入字符后按下Enter键之后,会在缓冲区中留下一个换行符'\n'。这个换行符会被下一个scanf函数读取,导致输入被跳过。可以在scanf函数前加上getchar函数,以消耗掉缓冲区中的换行符。
- 在使用scanf函数读取字符时,需要在格式字符串中加上空格,以跳过换行符或空格。 修改后的代码如下:
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char another_game = 'Y';
printf("\n扮演简单西蒙");
printf("注意屏幕上的数字序列");
printf("\n小心观察,因为数字只显示一秒钟 !");
printf("\n编译器会重新编译它们,然后提示您");
printf("输入相同的序列");
printf("\n当你这样做时,你必须在数字之间加空格.\n");
printf("\n祝你好运\n按Enter键播放\n");
getchar(); // 消耗掉缓冲区中的换行符
scanf("%c", &another_game);
do
{
printf("\n你想再玩一次吗 (y/b)? ");
getchar(); // 消耗掉缓冲区中的换行符
scanf("%c", &another_game);
} while (toupper(another_game) == 'Y');
return 0;
}
``
原文地址: https://www.cveoy.top/t/topic/hZFT 著作权归作者所有。请勿转载和采集!