c - C - 如何在 ay/n 循环中排除其他输入
问题描述
所以我试图让一个简单的 y/n 输入工作。我让它工作了,当输入 n 或 N 时它会中断 while(1) 循环,但是,任何其他字母字符都会将循环设置为再次进行。我只希望它在输入 Y 时循环。
我试过:
if (try_another != 'n' || try_another != 'N' || try_another != 'y' || try_another != 'Y')
,在!isalpha
线路之后,这没有用。
我试过scanf (" %c", try_another);
然后比较分配的字符。
这是我当前的代码设置:
printf("Do you wish to try another problem [y/n]: ");
do{
try_another = getchar();
/*keeps scanning for input until its a letter*/
}while(!isalpha(try_another));
/*when input is n or N it will end the program*/
if (try_another == 'n' || try_another == 'N'){
break;
}
解决方案
你为什么不这样做?
do {
try_another = getchar();
} while(try_another != 'n' && try_another != 'N' && try_another != 'y' && try_another != 'Y');
你想在角色不是这个和那个的时候循环。你很亲密,但不小心写了“或”而不是“和”。
推荐阅读
- php - 多个计数和求和 MySQL 函数在 MySQL 结果中使用多个连接返回错误值
- google-bigquery - BigQuery - 在连接谓词中使用 SQL UDF
- git - 如何实现 git-hook 以使用“无快进”更改合并消息?
- unity3d - 掷骰子模拟:降落在边缘?
- c++ - a[i] + 1 对 a[N][N] 表示什么
- r - R 3.5.1,找不到函数“dbConnect”
- nsis - 如何使用 NSIS 文本框、按钮事件?
- chocolatey - 如何重新安装巧克力包
- android - 检测静音|在 Android TV 上取消静音
- integer-division - 试图找到商和余数的 Knuth 讨论