因为你的getchar和scanf冲突了,要输入两次,我改了一下#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void main()
{
int a, b, numOfQues = 0, numOfRignt = 0;
char inquiry;
int op;
srand(time(NULL));
printf("***欢迎你来做两位数的加减法***\n");
do
{
int temp = rand() % 2;
a = rand() % 100;
b = rand() % 100;
switch (temp)
{
case 0:
{
int ans;
printf("%d + %d = ", a, b);
scanf("%d", &ans);
if (ans == a + b)
{
printf("恭喜!答对了!\n");
numOfRignt++;
}
else
{
printf("答错了,再接再厉!\n");
}
}
numOfQues++; // 统计做题次数
case 1:
{
int ans;
printf("%d - %d = ", a, b);
scanf("%d", &ans);
if (ans == a - b)
{
printf("恭喜!答对了!\n");
numOfRignt++;
}
else
{
printf("答错了,再接再厉!\n");
}
}
}
numOfQues++; // 统计做题次数
fflush(stdin);
printf("你还要做一题吗?(N表示不要):\n");
// getchar();
scanf("%c", &inquiry);
printf("%c\n", inquiry);
op = (inquiry == 'N' || inquiry == 'n')?0:1;
} while (op);
printf("你的答题正确率为 %.2f 。再见!\n", 100.0 * (float)numOfRignt / (float)numOfQues);
}
|