欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

完成猜数字游戏

程序员文章站 2024-03-18 17:01:40
...
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdlib.h>
#include<time.h>  
#include<stdio.h>  
int menu()  
{
    int choose = 0;
    printf("**************************\n");
    printf("Please choose:\n");
    printf("1.Game\n");
    printf("0.EXIT\n");
    printf("**************************\n");
    while (1)
    {
        scanf("%d", &choose);
        if (1 == choose)
        {
            return 1; 
        }
        else if (0 == choose)
        {
            return -1; 
        }
        else
        {
            printf("Please choose again!\n");  
        }
    }
}
void Game()   
{
    int num = 0;
    int r = 0;
    srand((unsigned)time(NULL));  
    r = rand() % 100; 
    do
    {
        printf("Please input the number you guess:\n");
        scanf("%d", &num);
        if (num > r)
        {
            printf("It is big.\n");
        }
        else if (num < r)
        {
            printf("It is small.\n");
        }
        else
        {
            printf("Congratulations, you guessed it!\n");
            break;
        }
    } while (1);  
}
int main()
{
    int ret = 0;
    while (1)
    {
        ret = menu();
        if (1 == ret)
        {
            Game();
        }
        else
        {
            break;
        }
    }
    return 0;
}

运行结果如下:
完成猜数字游戏