首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >猜随机数冷热游戏输出

猜随机数冷热游戏输出
EN

Stack Overflow用户
提问于 2014-07-18 04:58:10
回答 4查看 3.4K关注 0票数 0

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

  1. 1 to 100中生成一个随机的秘密数字,并要求用户猜出这个数字。
  2. 如果用户猜测正确,则打印猜测是正确的。
  3. 首先猜测一下,如果它不正确,它应该说热。
  4. 告诉用户,如果他的猜测越来越接近这个秘密,他就会变得越来越热。
  5. 或者如果他的猜测离得更远。
  6. 游戏继续进行,直到猜到秘密号码为止。

我当前的程序在猜测是热还是冷方面给出了不正确的输出,也不确定我应该把({放在哪里,如果有的话,或者在哪里使用"if""else if"。如果不是太多,请解释变化。

代码语言:javascript
运行
复制
#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;
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-07-18 06:07:03

下面是代码,有几件事你需要知道:首先,你需要比较数字之间距离的绝对值,因为当你减法得到距离时,你可以得到一个负数。

while (first_guess != secret)第二,你不应该比较一个你还没有读过的值。

不需要将{}放在IFs之外,您需要在if语句之后使用它。也试着改进一下你的压痕。

最后,在回答您的问题时,最重要的建议是,如果您有一个检查某些条件的if,则不需要使用另一个if来检查opossite条件--如果只有两个条件是正的,只需使用else,如果需要的话,在第一个else中使用其他的if else

示例:

错误的方式:

代码语言:javascript
运行
复制
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);
}

解决办法是:

代码语言:javascript
运行
复制
#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');,以确保用户的每个输入都被正确读取。

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

代码语言:javascript
运行
复制
#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);
}
票数 0
EN

Stack Overflow用户

发布于 2014-07-18 05:56:16

你的代码

代码语言:javascript
运行
复制
if (first_guess != secret)
  printf("Your guess was hot enter your next guess from (1 to %d)?\n", 
    MAX_VAL);
  scanf("%d", &next_guess);

如果-语句只会影响printf-语句,而不会影响scanf语句,则不会执行您认为它所做的操作。你应该把语句用大括号括起来。

代码语言:javascript
运行
复制
if (first_guess != secret)
{
  printf("Your guess was hot enter your next guess from (1 to %d)?\n", 
    MAX_VAL);
  scanf("%d", &next_guess);
}

对于键盘输入,我不太喜欢sc如f(),所以我将使用fget(),在这里指定最大值缓冲区,然后将值转换为整数,或者使用scanf()进行转换。

代码语言:javascript
运行
复制
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中的所有变量

代码语言:javascript
运行
复制
int next_guess = 0, first_guess = 0, secret, distance = 0;
票数 1
EN

Stack Overflow用户

发布于 2014-07-18 05:44:54

我不知道为什么这个游戏你需要两个猜测。这将是我的版本:

代码语言:javascript
运行
复制
#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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24817489

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档