在C中创建一个时循环

发布于 2025-02-02 02:59:57 字数 409 浏览 4 评论 0原文

我刚刚开始学习c,我决定创建一个时循环,以确保我掌握了事情。 这是我的代码:

#include <stdio.h>

void main(){
    int num, guess, turns;
    num = 13;
    guess = getchar();
    turns = 0;
    
    while(guess != num){
        printf("%d", guess);
        ++turns;
        printf("Turns:\n");
        printf("%d", turns);
        guess;
    }      
}

它给了我一个无限的循环。有人可以告诉我我在哪里做错了吗?另外,如果您有任何建议或技巧,请随时离开它们。

I've just started learning C and I've decided to create a while loop to make sure I get the hang of things.
This is my code:

#include <stdio.h>

void main(){
    int num, guess, turns;
    num = 13;
    guess = getchar();
    turns = 0;
    
    while(guess != num){
        printf("%d", guess);
        ++turns;
        printf("Turns:\n");
        printf("%d", turns);
        guess;
    }      
}

It gives me an infinite loop. Can someone please tell me where I went wrong? Also, if you have any suggestions or tips, please feel free to leave them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

○愚か者の日 2025-02-09 02:59:57

getChar()不会从终端读取,您必须使用:

scanf(" %d", &guess);

在while循环中,您必须再次从终端读取一个值(如果猜测是错误的)

while(guess != num){
   printf("Turns:\n");
   printf("%d", guess);
   ++Turns;
   scanf(" %d", &guess);
}
printf("%d is the correct guess", guess);

getchar() does not read from terminal, you have to use:

scanf(" %d", &guess);

In the while loop you have to read a value from terminal again (if the guess is wrong)

while(guess != num){
   printf("Turns:\n");
   printf("%d", guess);
   ++Turns;
   scanf(" %d", &guess);
}
printf("%d is the correct guess", guess);
爱给你人给你 2025-02-09 02:59:57

我在您的代码中发现了2个问题
首先是您正在使用
试图获得整数输入时,“ getchar”而不是“ scanf”。

第二个问题是,您没有更新while循环内部“猜测”的值(这导致了您的无限循环)。

为了方便起见
这是代码的固定版本,

void main() {
    printf("start \n");
    int num, guess, turns;
    num = 13;
    
    scanf("%d", &guess);
    turns = 0;
    
    while(guess != num){
        printf("your guess was : %d \n", guess);
        ++turns;
        printf("Turns:");
        printf("%d \n", turns);
        
        scanf("%d", &guess);
    }      
    
}

希望我能帮助:)

I spotted 2 problems in your code
the first is that you are using
"getchar" instead of "scanf" when trying to get an integer input.

And the second problem is that you are not updating the value of "guess" inside the while loop (and that's causing your infinite loop).

For your convenience
here is a fixed version of the code

void main() {
    printf("start \n");
    int num, guess, turns;
    num = 13;
    
    scanf("%d", &guess);
    turns = 0;
    
    while(guess != num){
        printf("your guess was : %d \n", guess);
        ++turns;
        printf("Turns:");
        printf("%d \n", turns);
        
        scanf("%d", &guess);
    }      
    
}

Hope I could help :)

深府石板幽径 2025-02-09 02:59:57

在您的时循环中,变量猜测也不会更改

while(guess != num){
    printf("%d", guess);
    ++turns;
    printf("Turns:\n");
    printf("%d", turns);
    guess;
}  

,此行

    guess;

没有效果。

而且此语句在while循环之前

guess = getchar();

没有意义,因为函数getchar仅读取一个字符并返回字符的内部表示的值。

还要根据C标准注意,如果

int main( void )

您的程序可以看以下方式,则无参数的函数主函数。

#include <stdio.h>

int main( void )
{
    unsigned int num = 13;
    unsigned int turns = 0;
    
    unsigned int guess = 0;

    puts( "Try to guess the number I thought." );
    printf( "Enter a non-negative number: " );

    while( scanf( "%u", &guess ) == 1 && guess != num )
    {
        printf( "%u is not my number.\n", guess );
        ++turns; 
        printf( "It is your %u attempt\n", turns );
        printf( "\nEnter a non-negative number: " );
    }

    if ( guess == num )
    {
        printf( "\nYou have guessed the number using %u guesses.\n", turns );
    }
    else
    {
        printf( "\nYou have not guessed the number using &u guesses.\n", turns );
    }
}

Within your while loop the variable guess is not being changed

while(guess != num){
    printf("%d", guess);
    ++turns;
    printf("Turns:\n");
    printf("%d", turns);
    guess;
}  

Moreover this line

    guess;

does not have an effect.

And this statement before the while loop

guess = getchar();

does not make a sense because the function getchar reads only one character and returns the value of the internal representation of the character.

Also pay attention to that according to the C Standard the function main without parameters shall be declared like

int main( void )

Your program can look the following way

#include <stdio.h>

int main( void )
{
    unsigned int num = 13;
    unsigned int turns = 0;
    
    unsigned int guess = 0;

    puts( "Try to guess the number I thought." );
    printf( "Enter a non-negative number: " );

    while( scanf( "%u", &guess ) == 1 && guess != num )
    {
        printf( "%u is not my number.\n", guess );
        ++turns; 
        printf( "It is your %u attempt\n", turns );
        printf( "\nEnter a non-negative number: " );
    }

    if ( guess == num )
    {
        printf( "\nYou have guessed the number using %u guesses.\n", turns );
    }
    else
    {
        printf( "\nYou have not guessed the number using &u guesses.\n", turns );
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文