Player guessing game (v1.0)

Subject requirements:

1. The system randomly generates an integer between 0 and 99;

2. The player enters an integer, and if it matches the number randomly generated by the system, one, two, and third prizes can be randomly drawn (as determined by the system);

3. If the number entered does not match the number randomly generated by the system, the system will prompt the size according to the value given by the player;

4. The player has a total of ten chances to guess the number. If the player still fails to guess after ten times, the system prompts that the opportunity has been used up.

[Summary of running problems] ① If you change the magic and guess variables in the f() function to char type, no matter how many times you enter the value that fails to match, the value of the count variable i always stays at 1, instead of being as expected. Add 1. ②Although the rand() function can generate random numbers, the random numbers generated every time the program is run are the same. 

[Note] In order to save the main memory space, I store all the values ​​with a small value range in char type variables.

Problem procedure:

#include<stdio.h>
#include<stdlib.h>
char f()//用户猜数流程
{
    char magic=rand()%100;//生成一个介于0~99间的整数(包括0和99)
    char guess;//记录用户输入的整数
    char i=0;//循环计数变量
    printf("当前次数:\n");
    while(i<10)
    {
        printf("%d. 请输入一个介于0~99之间的整数: ", i+1);
        //printf("i=%d\n", i);//调试语句
        scanf("%d", &guess);
        if(guess==magic)
        {
            printf("猜对了!\n");
            return 1;
        }
        else if(guess>magic)
        {
            printf("猜大了!\n");
        }
        else
        {
            printf("猜小了!\n");
        }
        i++;
    }
    //若程序执行至此, 说明10次猜数机会已经用完.
    return 0;
}
char f2()//若用户猜对, 此程序随机决定用户的获奖等级
{
     char c=rand()%100;//随机生成一个0~99的整数
     if(c<=5)
     {
        return 1;
     }//一等奖
     else if(c<=30)
     {
         return 2;
     }//二等奖
     else
     {
         return 3;
     }//三等奖
}
int main()
{
    if(f()==1)
    {
        printf("恭喜您, 奖品到手!\n");
        if(f2()==1)
        {
            printf("一等奖.\n");
        }
        else if(f2()==2)
        {
            printf("二等奖.\n");
        }
        else
        {
            printf("三等奖.\n");
        }
    }
    else
    {
        printf("很遗憾, 您的10次机会已经用完, 欢迎再次参与!\n");
    }
    //
    return 0;
}

 

Correct procedure:

#include<stdio.h>
#include<stdlib.h>
char f()//用户猜数流程
{
    int magic=rand()%100;//生成一个介于0~99间的整数(包括0和99)
    int guess;//记录用户输入的整数
    char i=0;//循环计数变量
    printf("当前次数:\n");
    while(i<10)
    {
        printf("%d. 请输入一个介于0~99之间的整数: ", i+1);
        //printf("i=%d\n", i);//调试语句
        scanf("%d", &guess);
        if(guess==magic)
        {
            printf("猜对了!\n");
            return 1;
        }
        else if(guess>magic)
        {
            printf("猜大了!\n");
        }
        else
        {
            printf("猜小了!\n");
        }
        i++;
    }
    //若程序执行至此, 说明10次猜数机会已经用完.
    return 0;
}
char f2()//若用户猜对, 此程序随机决定用户的获奖等级
{
     char c=rand()%100;//随机生成一个0~99的整数
     if(c<=5)
     {
        return 1;
     }//一等奖
     else if(c<=30)
     {
         return 2;
     }//二等奖
     else
     {
         return 3;
     }//三等奖
}
int main()
{
    if(f()==1)
    {
        printf("恭喜您, 奖品到手!\n");
        if(f2()==1)
        {
            printf("一等奖.\n");
        }
        else if(f2()==2)
        {
            printf("二等奖.\n");
        }
        else
        {
            printf("三等奖.\n");
        }
    }
    else
    {
        printf("很遗憾, 您的10次机会已经用完, 欢迎再次参与!\n");
    }
    //
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_42048463/article/details/115168733