Mr.Vsence 2020-05-20 21:39 采纳率: 100%
浏览 107
已采纳

新手求教 这个程序怎么写

登录时填写检查ID 用int check()进行测试。
每次check()被调用时,都会询问密码,并返还是否一致 为0和1。密码假定为1234。
当check()被呼叫3次以上,如果ID不一致时,
check()会输出“登录时超过次数”的信息。
check()在函数中宣布静态区域变量 后使用。
-备注:在静态局部变量中存储尝试次数。当用户登录时,该局部变量增加1。
图片说明

翻译如下:
密码:1234
正确
。。。。

错误的密码
密码:1222
错误的密码
密码:1233
错误的密码
登录次数超过上限
。。。。

  • 写回答

1条回答 默认 最新

  • threenewbee 2020-05-20 23:50
    关注
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int check()
    {
        static int tried = 0;
        char ch[100];
        printf("密码:");
        scanf("%s", ch);
        int result = strcmp(ch, "1234") ? 0 : 1;
        if (!result)
        {
            printf("错误的密码\n");
            tried++;
            if (tried >= 3)
            {
                printf("登录次数超过上限\n");
                exit(0);
            }
        }
        if (result)
        {
            printf("正确\n");
        }
        return result;
    }
    
    int main()
    {
        while (!check());
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?