随即一个四位数,让用户输入一个四位数,判断输入是否正确,不正确一直允许输入,直到结束。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<stdlib.h>
#include <time.h>
int main()
{
srand((unsigned int)time(NULL));
int a[4] = { 0 };
int b[4] = { 0 };
int num = 0;
char zq = 't';
for (int i = 0; i < 4; ++i)
{
a[i] = rand() % 10;
}
printf("请输入一个4位数\r\n");
while (1)
{
zq = 't';
scanf("%d", &num);
fflush(stdin);
b[0] = num / 1000; //num=1234,b[0] =1
num = num - b[0] * 1000; //num=234
b[1] = num / 100; //num=234,b[1] =2
num = num - b[1] * 100; //num=34
b[2] = num / 10; //num=34,b[2] =3
b[3] = num - b[2]*10; //b[3]=4
for (int i = 0; i < 4; ++i)
{
if (a[i] == b[i])
{
printf("第%d位输入正确\r\n", i+1);
}
else
{
if (a[i] > b[i])
{
printf("第%d位输入的数字小了\r\n", i+1);
zq = 'f';
}
else
{
printf("第%d位输入的数字大了\r\n", i+1);
zq = 'f';
}
}
}
if (zq == 't')
{
break;
}
}
system("pause");
return 0;
}