C语言打字游戏

1)输入过程中无法推出哦

2)请按所给字母敲击键盘哦

3)按任意键开始测试,按下首字母则开始记时

4)输入出错则以“_”表示

5)空格再玩一次,“ESC”退出

(学习内容)

#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <stdlib.h>
#include <corecrt.h>
void notice(void)
{
    printf("*********************************************************\n");
    printf("    1)输入过程中无法推出哦\n");
    printf("    2)请按所给字母敲击键盘哦\n");
    printf("    3)按任意键开始测试,按下首字母则开始记时\n");
    printf("    4)输入出错则以“_”表示\n");
    printf("    5)空格再玩一次,“ESC”退出\n");
    printf("*********************************************************\n");
}
int main()
{
    char ch;
    char str[51]="";//初始化,50个字母,'\0'
    int i;
    int count = 0;
    time_t start_time, end_time;
    while (1)//1:死循环
    {
        system("cls");//清屏
        notice();
        ch = _getch();//获取键盘的字符
        srand((unsigned)time(NULL));//获取秒数,同一秒产生的种子相同
        for (i = 0; i < 50; i++)//输出50个字母
        {
            str[i] = rand() % 26 + 'a';
        }
        str[50] = '\0';
        printf("%s\n", str);
        //获取键盘的字符
        count = 0;//count的值清零
        for (i = 0; i < 50; i++)
        {
            ch = _getch();
            if (i == 0)
            {
                start_time = time(NULL);
            }
            if (ch == str[i])
            {
                count++;
                printf("%c", ch);
            }
            else
            {
                printf("_");
            }
        }
        end_time = time(NULL);
        printf("\n正确率为 %d %c\n", count*100/50,'%');
        printf("用时 %ld 秒\n", (long int)end_time - start_time);
        while (1)
        {
            ch = _getch();
            if (ch == ' ')
            {
                break;
            }
            if (ch == 27)//字符ESC
            {
                return 0;
            }
        }
    }
    return 0;
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值