猜猜随机数冷热游戏输出

时间:2014-07-18 04:58:11

标签: c random codeblocks

尝试使用以下代码块语言构建程序:

  1. 1 to 100生成随机密码,并要求用户猜出该号码。
  2. 如果用户猜对了打印猜测是正确的。
  3. 如果第一次猜测不正确则应该说热。
  4. 告诉用户如果他的猜测越来越接近这个秘密,他就会变热。
  5. 如果他的猜测更远,他还是冷的。
  6. 游戏一直持续到秘密号码被猜到。
  7. 我当前的程序提供了关于猜测是热还是冷的错误输出,也不确定我应该放置( 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;
    }
    

4 个答案:

答案 0 :(得分:1)

你的代码

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;

答案 1 :(得分: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;
}

答案 2 :(得分: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;
}

答案 3 :(得分:0)

以下是代码,您需要了解一些事项: 首先,你需要比较数字之间的距离的绝对值,因为当你减去距离时,你可以获得一个负数。

while (first_guess != secret) 第二,你不应该比较你已经读过的价值。

无需将{}放在IF之外,您需要在if句后使用它。也尝试稍微改善你的缩进。

最后,最重要的建议,回答你的问题是,如果你有if检查某些条件,你不需要再设置if来检查opossite条件如果只有两个条件是posibble,只需使用else,如果需要,请在第一个if else内使用其他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 ((clean=getchar())!='\n');以确保用户的每个输入都被正确加入。

如果宏导致问题,这里是一个使用函数计算绝对值而不是宏的实现:

#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);
}