简体   繁体   English

C-while循环不会退出

[英]C - while loop will not exit

I'm creating a rock, paper, scissors code for a school lab. 我正在为学校实验室创建一块石头,纸,剪刀代码。 I'm just trying to test for invalid entries, but I get stuck in a continuous while loop despite entering a valid character. 我只是想测试无效条目,但是尽管输入了有效字符,但我仍陷于连续的while循环中。

printf("\nEnter (r)ock, (p)aper, or (s)cissors. Enter (q) to quit.");
scanf("%c", &input);
if(input=='Q' || input=='q') {
     return 0;
}
while(input!='R' || input!='r' || input!='P' || input!='p' || input!='S' || input!='s' || input!='Q' || input!='q') {
        printf("\nInvalid entry! Please enter (r)ock, (p)aper, or (s)cissors. Enter (q) to quit.");
        scanf(" %c", &input);
}

Any help would be appreciated! 任何帮助,将不胜感激!

You need to change all the OR operators in the while loop to AND operators. 您需要将while循环中的所有OR运算符更改为AND运算符。 If they are OR operators, all inputs would be true. 如果它们是OR运算符,则所有输入均为true。

` `

printf("\nEnter (r)ock, (p)aper, or (s)cissors. Enter (q) to quit.");
scanf("%c", &input);
if(input=='Q' || input=='q') {
     return 0;
}
while(input!='R' && input!='r' && input!='P' && input!='p' && input!='S' && input!='s' && input!='Q' && input!='q') {
        printf("\nInvalid entry! Please enter (r)ock, (p)aper, or (s)cissors. Enter (q) to quit.");
        scanf(" %c", &input);
}

` `

It seems you've got a classical logic bug in your code. 看来您的代码中存在经典的逻辑错误。 It took me a while to catch it too, because using or ( || ) seems to make sense. 我也花了一段时间才抓住它,因为使用或( || )似乎很有意义。 In this case, though, if you think about it, the computer is following your instructions correctly and never allowing you to leave the loop. 但是,在这种情况下,如果您考虑一下,计算机将正确地遵循您的说明,并且永远不会让您离开循环。 What you really mean is that if your input is different than 'R' and ( && ) 'r' and 'P' and 'p'... it is not valid. 您真正的意思是,如果您输入的内容不同于'R'和( && )'r'以及'P'和'p'...,那是无效的。

On another, unrelated topic, it is probably a good idea to always check the return value of your input function calls. 在另一个不相关的主题上,始终检查输入函数调用的返回值可能是一个好主意。 In this case you may want to write something like 在这种情况下,您可能需要编写类似

while (1) { /* infinite loop */
    printf("\nEnter (r)ock, (p)aper, or (s)cissors. Enter (q) to quit.");
    if (scanf(" %c", &c) != EOF) {
        if (c == 'q' || c == 'Q') {
            break;
        } else if (c == 'r' || c == 'R') {
            /* rock */
        } else if (/* paper, etc... */) {
            /* ... */
        } else {
            /* you can say here that the input is not valid */
        }
    } else {
        printf("\nBye!\n");
        break;
    }
}

As a 'bonus' if you're using a *NIX terminal (think Linux or BSD) you'll be able to leave the loop by typing Ctrl-D (^D), which will cause the scanf to return EOF. 作为“奖励”,如果您使用* NIX终端(例如Linux或BSD),则可以通过键入Ctrl-D(^ D)退出循环,这将导致scanf返回EOF。 Remember to always be very careful if you ever write an infinite loop (like this while(1) loop on my example). 请记住,如果您曾经写过无限循环(例如在我的示例中,while(1)循环),请务必格外小心。 You'll have to provide some way to get out of those loops. 您必须提供一些摆脱这些循环的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM