简体   繁体   English

为什么我的 do/while 循环在 C 中无法正常工作?

[英]Why does my do/while loop not work properly in C?

#include <cs50.h>
#include <stdio.h>

int main(void)
{
    int n = get_int("Please input a number between 1 and 8.\n");
    if( n >= 1 && n <= 8 )
    {
        printf( "Thank you for choosing within the boundaries. The number you entered was %i.\n", n );
    }

    do 
    {
        get_int( "Please enter a number that matches the critera.\n" );
    }
    while ( n > 8 || n < 1);
}

I just started off by listing my code right off the bat.我刚开始列出我的代码。 Please go easy on me, I'm a newbie at this and am taking CS50 on edX to become better.请 go 对我轻松一点,我是这方面的新手,正在 edX 上使用 CS50 以变得更好。 When I enter an integer that "matches the criteria" (is between 1 and 8, inclusive), my code works like its supposed to.当我输入一个“符合条件”(介于 1 和 8 之间,含)的 integer 时,我的代码按预期工作。 Similarly, when I enter an integer that is beyond those boundaries, it works like tis supposed to.同样,当我输入超出这些边界的 integer 时,它的工作方式与预期的一样。 However, after the prgram reprompts me to enter another integer, this time with me entering an integer within the boundary, the code does not work like it should.但是,在 prgram 再次提示我输入另一个 integer 之后,这次我在边界内输入了一个 integer,代码无法正常工作。 Instead of just saying "Thank you for choosing within the boundaries. The number you entered was %i", it says "Please enter a number that matches the critera", even though I entered something within the boundaries.即使我在边界内输入了一些内容,它也不会只是说“感谢您在边界内选择。您输入的数字是 %i”,而是说“请输入与标准匹配的数字”。

I hope you guys will be able to help.我希望你们能提供帮助。 Thanks.谢谢。

I think that in the line of "do while" the variable n is missing before get _...., I don't use cs50.h but is very important and c the use of variable also, hopefully run the code我认为在“do while”行中,变量 n 在 get _.... 之前丢失了,我不使用 cs50.h 但非常重要,c 也使用变量,希望运行代码

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

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