不能比&QUOT其他的结果;过高"在C一个猜谜游戏 [英] Cannot get a result other than "Too High" for a guessing game in c

查看:108
本文介绍了不能比&QUOT其他的结果;过高"在C一个猜谜游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成我code代表的程序,其允许用户输入值的范围从猜测,则随机生成的号码,然后允许用户猜测和报告过低,过高,或正确。当我测试运行程序,我只得到输出过高。我已经试过调试,看着我的code,但我看不出哪里出了问题。任何帮助深表AP preciated!

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;诠释的main()
{    诠释最小值,最大值,数,猜,计数= 0;    的printf(请输入两个整数的范围:\\ n);
    scanf函数(%D,&安培;分钟,和放大器;最大);    数=(分+兰特()%(最大 - 最小+ 1));
    的printf(我想在%d和%d之间的数\\ n,最小值,最大值);    的printf(请输入您的猜测:\\ n);
    scanf函数(%d个,&安培;猜的);    而(猜!=号)
    {        如果(猜<号)
        {
            算上++;
            的printf(太低\\ n);
            的printf(请输入您的猜测:\\ n);
            scanf函数(%d个,&安培;猜的);
        }        其他
        {
            算上++;
            的printf(过高\\ n);
            的printf(请输入您的猜测:\\ n);
            scanf函数(%d个,&安培;猜的);
        }
    }        算上++;
        的printf(正确的,你花了%d个试图猜测我的电话号码。算);返回0;
}


解决方案

您的前pression为数量可以产生价值的方式超出范围。它应该是:

 数=分钟+兰特()%(最大 - 最小+ 1);

您还需要采取措施来初始化随机数发生器。

I have finished my code for a program that allows a user to enter a range of values to guess from, then randomly generates the number, and then allows the user to guess and reports too low, too high, or correct. When I test run the program, I only get outputs of "Too high." and I've tried debugging and looking at my code but I can't see where it went wrong. Any help is much appreciated!

#include <stdio.h>
#include <stdlib.h>

int main()
{

    int min, max, number, guess, count = 0;

    printf("Please enter two integers for the range:\n");
    scanf("%d %d", &min, &max);

    number = (min + rand()%(max - min + 1));
    printf("I'm thinking of a number between %d and %d.\n", min, max);

    printf("Enter your guess:\n");
    scanf("%d", &guess);

    while(guess != number)
    {

        if(guess < number)
        {
            count++;
            printf("Too low.\n");
            printf("Enter your guess:\n");
            scanf("%d", &guess);
        }

        else
        {
            count++;
            printf("Too high.\n");
            printf("Enter your guess:\n");
            scanf("%d", &guess);
        }
    }

        count++;
        printf("Correct, it took you %d tries to guess my number. ", count);

return 0;
}

解决方案

Your expression for number can generate values way out of range. It should be:

number = min + rand() % (max - min + 1);

You also need to take steps to initialize your random number generator.

这篇关于不能比&QUOT其他的结果;过高&QUOT;在C一个猜谜游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆