简体   繁体   English

While循环不会按C的预期中断

[英]While loop won't break as intended in C

I'm trying to learn how to program in C and have stumbled into a problem that seems like it should have been a simple fix, but it's giving me more issues then I anticipated. 我正在尝试学习如何使用C编程,并且偶然发现了一个似乎应该是简单的解决方案的问题,但是它给了我比我预期的更多的问题。 I'm trying to created a number guessing game, where you get three chances to guess the number, but my issue is that the Do While loop wont break when the right answer is guessed. 我正在尝试创建一个数字猜谜游戏,您有3次机会猜数字,但是我的问题是,当猜对了正确答案时,Do While循环不会中断。 Here is the function: 这是函数:

void Win_Search(int lucky[],const int MAX, int user_entry, int i)
{
    int j=0;

    do {
        j++;
        printf("Please enter a number between 0 and 100\n");
        scanf("%d",&user_entry);

        for(i = 0; i < MAX; i++)
        {
            if(user_entry==lucky[i])
            {
                printf("winner\n");
            }
        }
    } while(user_entry==lucky[i]||j<3);

}

Basically it's supposed to loop through the array lucky[i] and check to see if the user_entry equals any of the 20 numbers in the array. 基本上,应该遍历数组lucky [i]并检查user_entry是否等于数组中20个数字中的任何一个。 As of right now it loops through, recognizes if a winning number has been selected from the array, but doesn't break from the array. 截至目前,它循环遍历,识别是否已从阵列中选择了中奖号码,但未从阵列中中断。

when I change it to 当我将其更改为

}while(user_entry!=lucky[i]||j<3);  

it completely ignores the counter and just loops forever. 它完全忽略了计数器,只是永远循环。

I don't want to use break because everything I've read about it talks about it's poor programming practice. 我不想使用break,因为我所读到的所有内容都谈到了不良的编程实践。 Is there another way to break, or have simply just made a mistake thats causing this issue. 还有另一种方法可以打破,或者只是犯了一个错误而导致了此问题。

Thanks in advance. 提前致谢。

Consider for a second where your index variable "i" comes from. 考虑一下索引变量“ i”的来源。 What happens to it after you've found a correct user entry? 找到正确的用户条目后会发生什么? Where does the control flow go? 控制流向何处去?

I would suggest having a look at the "break" keyword. 我建议看看“ break”关键字。

You wrote while (user_entry == lucky[i]..) which translates to as long as user_entry is equal to lucky[i] keep on looping . 您写了while (user_entry == lucky[i]..) ,这意味着as long as user_entry is equal to lucky[i] keep on looping Which is clearly not what you intend to do. 这显然不是您打算做的。

Transform your condition to } while (user_entry != lucky[i] && j < 3); 将您的条件转换为} while (user_entry != lucky[i] && j < 3); and you should be fine. 你应该没事的 This will translate in plain english to as long as user_entry is different of lucky[i] AND j is inferior to 3, keep looping . as long as user_entry is different of lucky[i] AND j is inferior to 3, keep looping这将以普通英语翻译。

But using this, you test on the value of lucky[i] even when i means nothing ( when i is equal to max, you don't want to test it, and this goes in the domain of undefined behavior ). 但是使用它,即使i没有任何意义,您也可以测试lucky[i]的值(当i等于max时,您不想对其进行测试,这属于未定义行为的领域)。

But if you realy dont want to use break keyword, one solution is to use a flag. 但是,如果您确实不想使用break关键字,则一种解决方案是使用标志。 Set it to 1 before you start to loop, and change it to 0 when the good answer is found. 开始循环之前将其设置为1,找到良好答案后将其更改为0。 Your code will become 您的代码将成为

void Win_Search(int lucky[],const int MAX, int user_entry, int i)
{
    int j=0;
    char flag = 1;

    do {
        j++;
        printf("Please enter a number between 0 and 100\n");
        scanf("%d",&user_entry);

        for(i = 0; i < MAX; i++)
        {
            if(user_entry==lucky[i])
            {
                printf("winner\n");
                flag = 0;
            }
        }
    } while(flag&&j<3);

}

}while(user_entry!=lucky[i]||j<3);

That is bad logic - loop while the user's entry isn't the lucky number OR j is below three? 这是不好的逻辑-在用户输入的不是幸运数字或j小于3时循环播放? Surely you actually want this: 当然您确实想要这样:

}while(user_entry!=lucky[i]&&j<3);

This is only the solution to your second issue of it ignoring the counter - the main problem is solved in the other answers. 这只是第二个问题的解决方案,它忽略了计数器-主要问题在其他答案中得到解决。

The only independent condition is that the user has more guesses left. 唯一独立的条件是用户还有更多的猜测。 try this while" 尝试一下

while(j <= 3);

The less than should be obvious, but the equals belongs there because you increment your j before the loop so it will be 小于应该是显而易见的,但是等值属于那里,因为您在循环之前增加了j,所以它将

j = 1 => first guess j = 1 =>第一个猜测

j = 2 => second guess j = 2 =>第二个猜测

j = 3 => third guess j = 3 =>第三次猜测

After that the user should have no more guesses 之后,用户将不再有任何猜测

You should find this doesn't work, that is because we want to exit the loop if the user guesses correctly. 您应该发现这是行不通的,这是因为如果用户猜对了,我们想退出循环。 To do this, you can use a int as a bool (0-false, 1-yes). 为此,您可以将int用作布尔值(0-false,1-yes)。

void Win_Search(int lucky[],const int MAX, int user_entry, int i)
{
    int j=0;
    int exitCase = 0;
    do {
        j++;
        printf("Please enter a number between 0 and 100\n");
        scanf("%d",&user_entry);

        for(i = 0; i < MAX; i++)
        {
            if(user_entry==lucky[i])
            {
                exitCase = 1;
                printf("winner\n");
            }
        }
    } while(exitCase == 0 || j <= 3);

}

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

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