C 猜随机数冷热游戏输出

C 猜随机数冷热游戏输出,c,random,codeblocks,C,Random,Codeblocks,试图用代码块c语言构建一个程序,该程序: 生成一个从1到100的随机密码,并要求用户猜测该数字 如果用户猜测正确,则打印猜测是正确的 对于第一个猜测,如果它是不正确的,它应该说热 告诉用户,如果他的猜测越来越接近秘密,他会变得很热 或者,如果他的猜测离得更远,他会觉得很冷 游戏一直持续到猜出秘密号码为止 我当前的程序在猜测是热还是冷的问题上给出了不正确的输出,也不确定我应该把放在哪里(vs{如果有,或者在哪里使用“if”vs“else if”。如果不太多,请解释更改 #include <s

试图用代码块c语言构建一个程序,该程序:

  • 生成一个从
    1到100的随机密码,并要求用户猜测该数字
    
  • 如果用户猜测正确,则打印猜测是正确的
  • 对于第一个猜测,如果它是不正确的,它应该说热
  • 告诉用户,如果他的猜测越来越接近秘密,他会变得很热
  • 或者,如果他的猜测离得更远,他会觉得很冷
  • 游戏一直持续到猜出秘密号码为止
  • 我当前的程序在猜测是热还是冷的问题上给出了不正确的输出,也不确定我应该把
    放在哪里(
    vs
    {
    如果有,或者在哪里使用
    “if”
    vs
    “else if”
    。如果不太多,请解释更改

    #include <stdio.h>
    #include <stdlib.h>
    #define MAX_VAL 100
    
     int main(void)
    {
      int next_guess, first_guess, secret, distance;
      srand(time(0));
    
      secret = 1 + rand()%MAX_VAL;
      distance = (secret - first_guess);
    
    while (first_guess != secret) {
    printf(" enter your guess from (1 to %d)?\n", MAX_VAL);
    scanf("%d", &first_guess);
    
    {
    
    if (first_guess == secret)
        printf("You guessed correctly!\n");
    
    if (first_guess != secret)
        printf("Your guess was hot enter your next guess from (1 to %d)?\n", MAX_VAL);
        scanf("%d", &next_guess);
    
    if (next_guess == secret)
        printf("You guessed correctly!\n");
    
    if ((next_guess-secret) < distance)
        printf("Your guess was hot,");
    
    if ((next_guess-secret) >= distance)
         printf("Your guess was cold,");
    
       }
      }
      return 0;
    }
    
    #包括
    #包括
    #定义最大值100
    内部主(空)
    {
    int下一个猜测,第一个猜测,秘密,距离;
    srand(时间(0));
    secret=1+rand()%MAX\u VAL;
    距离=(秘密-第一次猜测);
    while(猜一猜!=秘密){
    printf(“输入您从(1到%d)的猜测?\n”,最大值);
    scanf(“%d”&第一次猜测);
    {
    if(first_guess==secret)
    printf(“您猜对了!\n”);
    如果(第一次猜!=秘密)
    printf(“您的猜测是热的,请输入从(1到%d)的下一次猜测?\n”,MAX_VAL);
    scanf(“%d”&下一个猜测);
    if(next_guess==secret)
    printf(“您猜对了!\n”);
    if((下一个猜-密)<距离)
    printf(“你的猜测很热,”);
    如果((下一个秘密)>=距离)
    printf(“你的猜测很冷淡,”);
    }
    }
    返回0;
    }
    
    我不知道为什么这个游戏需要两次猜测。这将是我的版本:

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define MAX_VAL 100
    
    int main(void)
    {
        int guess, secret, distance;
        srand(time(0));
    
        secret = 1 + rand()%MAX_VAL;
    
        while (true) 
        {
            printf(" enter your guess from (1 to %d)?\n", MAX_VAL);
            scanf("%d", &guess);
    
            if (guess == secret)
            {
                printf("You guessed correctly!\n");
                break;
            }
    
            else
            {
                distance = guess-secret;
    
                if (distance>0)
                    printf("Your guess was hot,");
    
                else
                    printf("Your guess was cold,");
            }
        }
    
        return 0;
    }
    
    #包括
    #包括
    #包括
    #定义最大值100
    内部主(空)
    {
    猜测、秘密、距离;
    srand(时间(0));
    secret=1+rand()%MAX\u VAL;
    while(true)
    {
    printf(“输入您从(1到%d)的猜测?\n”,最大值);
    scanf(“%d”,猜测(&guess);
    如果(猜测==秘密)
    {
    printf(“您猜对了!\n”);
    打破
    }
    其他的
    {
    距离=猜测秘密;
    如果(距离>0)
    printf(“你的猜测很热,”);
    其他的
    printf(“你的猜测很冷淡,”);
    }
    }
    返回0;
    }
    
    检查我去年试过的这个例子

    #include "stdio.h"
    #include "conio.h"
    #include "stdlib.h"
    #include "math.h" 
    #include "time.h"
    
    int main(void)
    {
        int n,a,count=0,l,score=0;
        char ch;
        do
        {
            count=0;
            srand(time(NULL));
            a=rand()%100+1; //RANGE OF NUMBER, here it is 1 to 100
            printf("Select  Level\n");
            printf("1)Easy\n");
            printf("2)Medium\n");
            printf("3)Hard\n");
            scanf("%d",&n);
            if(n==1)
                l=20;
            else if(n==2)
                l=10;
            else
                l=6;
            printf("Enter a number between 1 to 100\n");
            while(count!=l)
            {
                scanf("%d",&n);
                //system("cls");
                if(n==a)
                {
                    printf("\nYou win...!!!\n %d is the Correct Number.",n);
                    break;
                }
                else if(n>a)
                {
                    printf("\n%d is Too High...!!!",n);
                    count++;
                }
                else
                {
                    printf("\n%d is Too Low...!!!",n);
                    count++;
                }
                if(count==l)
                {
                    printf("\nCorrect Number is %d\n",a);
                    break;
                }
                printf("\nYou have %d chance left.\n",l-count); 
            }
            printf("Do You Want To Play More ... (Press y)");
            ch=getch();
        }
        while(ch=='y' || ch=='Y');
        getch();
        return 0;
    }
    
    你的代码

    if (first_guess != secret)
      printf("Your guess was hot enter your next guess from (1 to %d)?\n", 
        MAX_VAL);
      scanf("%d", &next_guess);
    
    如果不执行您认为的操作,if语句只会影响printf语句,而不会影响scanf语句。您应该将语句括在大括号中

    if (first_guess != secret)
    {
      printf("Your guess was hot enter your next guess from (1 to %d)?\n", 
        MAX_VAL);
      scanf("%d", &next_guess);
    }
    
    我不喜欢scanf()用于键盘输入,所以我会使用fgets()指定最大大小的缓冲区,然后将值转换为整数,或者使用sscanf()进行转换

    char buffer[32];
    fgets( buffer, sizeof(buffer), stdin );
    next_guess = atoi( buffer ); 
    
    or
    
    char buffer[32];
    fgets( buffer, sizeof(buffer), stdin );
    sscanf( buffer, "%d", &next_guess );
    
    您应该始终初始化C中的所有变量

    int next_guess = 0, first_guess = 0, secret, distance = 0;
    

    以下是代码,您需要了解以下几点: 首先,你需要比较两个数字之间距离的绝对值,因为当你用减法得到距离时,你可能会得到一个负数

    while(第一次猜!=secret)
    其次,不应该比较尚未读取的值

    没有必要把
    {}
    放在
    IF
    s之外,你需要在
    IF
    句子后面使用它。同时尝试稍微改进缩进

    最后,回答你的问题最重要的建议是,如果你有一个检查某个条件的
    if
    ,你不需要再放置另一个
    if
    来检查opassite条件如果只有两个条件是可行的,只需使用一个
    else
    ,如果需要,在第一个
    else中使用另一个
    if else

    例如:

    错误的方式:

    if (first_guess == secret)
        printf("You guessed correctly!\n");
    
    if (first_guess != secret) {
        printf("Your guess was hot enter your next guess from (1 to %d)?\n", MAX_VAL);
        scanf("%d", &next_guess);
    }
    
    right way:
    
    if (first_guess == secret)
        printf("You guessed correctly!\n");
    
    else {
        printf("Your guess was hot enter your next guess from (1 to %d)?\n", MAX_VAL);
        scanf("%d", &next_guess);
    }
    
    解决办法是:

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define MAX_VAL 100
    
    #define ABSOLUTE(a) (((a)>0)?(a):(-a))
    
    int main() {
        srand(time(0));
        int secret,new,newdistance,olddistance,ok;
        secret = 1 + rand()%MAX_VAL;
        printf ("secret is %d",secret);
        char clean;  
        do {
            printf(" enter your guess from (1 to %d)?\n", MAX_VAL);
            ok=scanf ("%d",&new);
            while ((clean=getchar())!='\n');
        } while(ok!=1);
        newdistance = (secret - new);
        do {
            if (new == secret)
            printf("You guessed correctly!\n");
            else {
                do {
                    printf("enter your guess from (1 to %d)?\n", MAX_VAL);
                    ok=scanf ("%d",&new);
                    while ((clean=getchar())!='\n');
                } while(ok!=1);
                if (new == secret)
                    printf("You guessed correctly!\n");
                else {
                    olddistance=newdistance;
                    newdistance=secret-new;
                    if (ABSOLUTE(newdistance) < ABSOLUTE(olddistance))
                        printf("Your guess was hot,");
                    else
                        printf("Your guess was cold,");
                }
            }
        } while (new!= secret);
        return (0);
    }
    

    在while循环中使用它之前,你应该先初始化
    。@Jayesh这不是一个完整的答案,只是一个注释。当你得到正确的数字时,你应该使用
    break;
    退出循环。@Jayesh这可能不是唯一的问题,我对调试它不感兴趣。如果你愿意,你可以。@RetiredNinja应该初始化你的意思是什么?我是编程新手。谢谢你,但它仍然会给出不正确的热输出或冷输出,因为距离可能最终为负数,你可以在距离为负数时靠近,但会变冷,因为,
    distance>0
    仅在
    distance=guess-secret
    为正数时才起作用。这就是我最初的原因lly进行了2次猜测。我在尝试运行时出错。第5行错误应在“/”之前出现token@user3723557尝试删除注释。可能您尝试编译的设置代码块不允许使用
    /
    注释。我在帖子中编辑了它。还添加了另一个不使用绝对值的解决方案实现值宏语法感谢您的帮助,我喜欢没有绝对值宏的1。第一次猜测中只有一个小问题,如果不正确,它应该是hot。@user3723557在这里,您让我思考了一下,但我认为它会起作用
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define MAX_VAL 100
    
    
    int absolute (int a) {
        return (a>0?a:-a);  
    }
    
    
    int main() {
        srand(time(0));
        int secret,new,newdistance,olddistance,ok,flagfirstguess;
        flagfirstguess=1;
        secret = 1 + rand()%MAX_VAL;
        printf ("secret is %d",secret);
        char clean;  
        do {
            printf(" enter your guess from (1 to %d)?\n", MAX_VAL);
            ok=scanf ("%d",&new);
            while ((clean=getchar())!='\n');
        } while(ok!=1);
        newdistance = (secret - new);
        do {
            if (new == secret)
                printf("You guessed correctly!\n");
            else {
                if (flagfirstguess==1) {
                    printf("Your guess was hot enter your next guess from (1 to %d)?\n", MAX_VAL);
                    flagfirstguess=0;
                }
                else {
                    do {
                        printf("enter your guess from (1 to %d)?\n", MAX_VAL);
                        ok=scanf ("%d",&new);
                        while ((clean=getchar())!='\n');
                    } while(ok!=1);
                    if (new == secret)
                        printf("You guessed correctly!\n");
                    else {
                        olddistance=newdistance;
                        newdistance=secret-new;
                        if (absolute(newdistance) < absolute(olddistance))
                            printf("Your guess was hot,");
                        else
                            printf("Your guess was cold,");
                    }
                }
            }
        } while (new!= secret);
       return (0);
    }