编写程序帮助小学生学习乘法,使用rand生成两个正的一位整数,然后程序应该显示诸如下面的问题:How much is 6 times 7?学生输入答案,程序针对学生的答案进行检查,如果正确则显示出“very good”,并且提出另一个乘法题,如果错误则显示“no”,然后让学生再次做同一道题知道得出最后正正确的答案。(用函数解决)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int n,a,b;
a=rand()%9+1;
b=rand()%9+1;
printf("How much is %d times %d?\n",a,b);
printf("输入答案(-1结束): ");
scanf("%d",&n);
while( n!=-1 )
{
if( n==a*b )
{
printf("Very good.\n");
srand(time(NULL));
a=rand()%9+1;
b=rand()%9+1;
printf("How much is %d times %d?\n",a,b);
printf("输入答案(-1结束): ");
scanf("%d",&n);
}
else
{
printf("NO.Please try again.\n");
printf("How much is %d times %d\n?",a,b);
printf("输入答案(-1结束): ");
scanf("%d",&n);
}
}
system("pause");
return 0;
}
这是我自己写 的 可是太多了 我想有个函数使得这个程序简单点 可是我自己想不出来
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void fun(int *a,int *b,int *c)
{
srand(time(NULL));
*a=rand()%10;
*b=rand()%10;
*c=(*a)*(*b);
}
void main()
{
int n,a,b,c;
fun(&a,&b,&c);
while(1)
{
printf("How much is %d times %d?\n",a,b);
printf("输入答案(-1结束): ");
scanf("%d",&n);
if(n==-1)break;
if( n==c )
{
printf("Very good.\n");
fun(&a,&b,&c);
}
else
{
printf("NOT .Please try again.\n");
}
}
}
#include <stdlib.h>
int main()
{
int n=0,a=rand()%9+1,b=rand()%9+1;
while( n!=-1 ){
printf("How much is %d times %d?\n",a,b);
printf("输入答案(-1结束): ");
scanf("%d",&n);
if( n==a*b ){
printf("\nVery good!\n");
a=rand()%9+1;
b=rand()%9+1;
}
else
printf("\nNO,Please try again!\n");
}
return 0;
}
这样改,不知道满不满意,我没用函数