登录时填写检查ID 用int check()进行测试。
每次check()被调用时,都会询问密码,并返还是否一致 为0和1。密码假定为1234。
当check()被呼叫3次以上,如果ID不一致时,
check()会输出“登录时超过次数”的信息。
check()在函数中宣布静态区域变量 后使用。
-备注:在静态局部变量中存储尝试次数。当用户登录时,该局部变量增加1。
翻译如下:
密码:1234
正确
。。。。
错误的密码
密码:1222
错误的密码
密码:1233
错误的密码
登录次数超过上限
。。。。
#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;
}