如何存储(scanf)随机生成的数字? (内部代码)

时间:2014-05-21 15:32:57

标签: c random scanf

这是我用来生成数字的代码:

printf("%d\n", rand()%10);

我正在创建一个纸牌游戏HiLo。我想知道如何存储从中生成的数字,然后让用户猜测下一个数字是高于还是低于前一个数字。之后,生成另一个数字,但尚未存储。我想比较新号码和旧号码,看看用户是否正确(很多点),然后存储新号码。

3 个答案:

答案 0 :(得分:1)

您只想使用变量。所有变量可能包括字母字符以及数字字符,但第一个字符必须是字母。创建变量的概念是,您正在为程序的成功运行创建临时存储区域。使用特定变量的任何代码区域都被视为依赖于它,变量在“范围”中起作用。范围使用{字符开始,并使用}字符结束。在这些字符之间声明的任何变量只能在这些字符中使用,而不能在它们之外使用,因为除了这些字符之外它们不存在。

我已经创建了一个程序示例,我相信您正在尝试编写该程序,以演示此类任务中涉及的软件编程实践。

示例如下所示:

#include <stdio.h>
#include <string.h>

#define LENGTH_OF_BUFFER 100

int main( int argc, char **argv )
{
    int prevRandomNumber = rand() % 10;
    int continuePlaying = 1;

    while( continuePlaying == 1 )
    {
        int randomNumber = rand() % 10;
        int userSelection = 0;
        char lineBuffer[LENGTH_OF_BUFFER];
        printf( "Previous random number %d\n", prevRandomNumber );

        while( userSelection == 0 )
        {
            printf( "higher or lower?" );
            gets( lineBuffer );

            if ( strcmp( "higher", lineBuffer ) == 0 )
            {
                userSelection = 1;
                printf( "You selected higher!\n" );
            }
            else if ( strcmp( "lower", lineBuffer ) == 0 )
            {
                userSelection = -1;
                printf( "You selected lower!\n" );
            }
            else
            {
                printf( "Sorry, I didn't understand you, please check your input and try again!\n" );
            }
        }

        if ( userSelection == 1 )
        {
            if ( randomNumber > prevRandomNumber )
            {
                printf( "Correct, you really are a marvel!\n" );
            }
            else
            {
                printf( "Incorrect, I pity you!\n" );
            }
        }
        else if ( userSelection == -1 )
        {
            if ( randomNumber < prevRandomNumber )
            {
                printf( "Correct, you really are a marvel!\n" );
            }
            else
            {
                printf( "Incorrect, I pity you!\n" );
            }
        }

        printf( "Number was %d\n", randomNumber );

        printf( "Care to play again?\n" );
        gets( lineBuffer );

        prevRandomNumber = randomNumber;

        if ( strcmp( "yes", lineBuffer ) != 0 )
        {
            continuePlaying = 0;
        }
    }
}

我希望这会有所帮助......

答案 1 :(得分:0)

int number = rand()%10;
int nextNumber;
char choice;
while(choice != 'Q' && choice != 'q')
{
    printf("Current Number is : %d",number);
    printf("\nYou want to guess the next number : ");
    printf("\nPress L if number will be greater than current number ");
    printf("\nPress S if number will be smaller than current number ");
    printf("\nPress Q if you want to quit playing : ");
    Printf("\n\nEnter your choice : ")
    scanf("%c",&choice);
    nextNumber = rand()%10;
    if(choice == 'L' || choice == 'l')
    {
        if(nextNumber > number)
        {
            printf("\nYour Guess is Right...");
        }   
        else
        {
            printf("\nYour Guess is wrong...");
        }
    }
    else if(choice == 'L' || choice == 'l')
    {
        if(nextNumber < number)
        {
            printf("\nYour Guess is Right...");
        }   
        else
        {
            printf("\nYour Guess is wrong...");
        }
    }
    else
    {
        printf("\nYour choice is invalid. Try Again...");
    }   
    number = nextNumber;
}

答案 2 :(得分:0)

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

int main(){
    char* bet, cmd;
    long random_number, new_random_number;
    while (1)
    {
        srand(time(NULL));

        random_number = rand();
        printf("%d\n", random_number);
        printf("bet if the next number is bigger (+) or lower (-): ");
        scanf("%s",bet);

        srand(time(NULL));
        new_random_number = rand();

        printf("%d\n", new_random_number);

        /* TODO - you might want to put that into a function some how */
        if ( bet[0] == '+')
        {
            if ( new_random_number >= random_number ) /* user is correct */
            {
                printf("yeah! you are right\n");
                printf("\nlet's play again!!\n");
                continue;
            }
            else
            {
                printf("trololol :P That was wrong\n");
                printf("\nlet's play again!!\n");
                continue;
            }
        }

        if ( bet[0] == '-')
        {
            if ( new_random_number < random_number ) /* user is correct */
            {
                printf("yeah! you are right\n");
                printf("\nlet's play again!!\n");
                continue;
            }
            else
            {
                printf("trololol :P That was wrong\n");
                printf("\nlet's play again!!\n");
                continue;
            }
        }
    }
}

/* vim: set et sw=4 ts=4: */