回复 楼主 wuwangxinan
#include<stdio.h>
#include <stdlib.h>
#include <time.h>
void main(void)
{
int choice,a,b,c,s;
char q='y';
printf("1 加法运算\n");
printf("2 减法运算\n");
printf("3 乘法运算\n");
printf("4 除法运算\n");
printf("0 退出\n" );
printf("请选择操作:");
scanf("%d",&choice);
while(q=='y')
{
switch (choice)
{
case 1:
srand(time(NULL));
a=rand()%10;
b=rand()%10;//随机产生个位数,可以自己修改数的范围
c=a+b;
printf("%d+%d= ",a,b);
scanf("%d",&s);
if(s==c)
printf("恭喜你,答对了!\n\n");
else
printf("答错了,继续努力哦!\n\n");
break;
case 2:
srand(time(NULL));
a=rand()%10;
b=rand()%10;
while(a<b)
{
a=rand()%10;//防止a<b
}
c=a-b;
printf("%d-%d= ",a,b);
scanf("%d",&s);
if(s==c)
printf("恭喜你,答对了!\n\n");
else
printf("答错了,继续努力哦!\n\n");
break;
case 3:
srand(time(NULL));
a=rand()%10;
b=rand()%10;
c=a*b;
printf("%d*%d= ",a,b);
scanf("%d",&s);
if(s==c)
printf("恭喜你,答对了!\n\n");
else
printf("答错了,继续努力哦!\n\n");
break;
case 4:
srand(time(NULL));
a=rand()%10;
b=rand()%10;
while(a%b!=0||b==0)//防止除不尽以及除数为0的情况
{
srand(time(NULL));
a=rand()%10;
b=rand()%10;
}
c=a/b;
printf("%d/%d= ",a,b);
scanf("%d",&s);
if(s==c)
printf("恭喜你,答对了!\n\n");
else
printf("答错了,继续努力哦!\n\n");
break;
case 0:
printf("欢迎下次使用,再见!\n\n");
exit(0);
default: printf("输入有误!\n\n");
}
printf("是否愿意做题?按y继续! \n");
scanf("%s",&q);
}
printf("欢迎下次使用,再见!\n");
}