初学者-字符验证循环

时间:2019-05-29 18:10:40

标签: c validation

我想问用户是否要播放“ [y | n]”,但是我的输入验证不起作用。我一直在为此绞尽脑汁,但似乎缺少一些基本知识。

代码正在读取值,但没有像我期望的那样通过布尔表达式。输入“ y”将显示“请输入'y'或'n'以继续:”

与链接推断的重复问题不同,我想对此控件使用WHILE循环,以允许多个错误的用户输入。

NB;游戏循环控制仅在验证用户[y | n]输入时出现问题。


printf("Would you like to play dice poker? y/n: ");
    scanf(" %c", &play);
    while (play == 'y'|| play== 'Y' || play == 'n' || play == 'N') {

    while (play != 'y'|| play != 'Y' || play != 'n' || play != 'N') {
        printf("input must be [y|n]\n");
        printf("please enter a 'y' or 'n' to continue:");
        scanf(" %c", &play);
        }

/*if/else loop to play or farewell and exit*/

/*Game Code - Works just fine*/


printf("Would you like to play again? [y|n] : "); 
       scanf(" %c", &playAgain);  

       while (playAgain != 'y' ||  playAgain != 'Y' || playAgain = 'n' || playAgain != 'N') {
         printf("input must be [y|n]\n");
         printf("please enter a 'y' or 'n' to continue:");
         scanf(" %c", &playAgain);
    }

     play = 'x'; //kick out of "play" loop

}

0 个答案:

没有答案