一个代码,用自定义函数出加法题,用自定义函数实现两个加数以及答案的输入,以及答
案是否正确的判断,并将判断结果作为函数返回值进行返回;在main函数里调用自定义函
数,并控制重做的次数。
明明一样的代码,为啥会报错呢,这俩代码就差了两行。
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
int a,b,c,s,n=0,m,k,z;
for(int i=1;i<=10;i++)
{
cout<<"两个加数分别为:";
int j= rand();
int k= rand();
a=j%10;
b=k%10;
cout<<"a="<<a<<",b="<<b<<endl;
s=a+b;
cout<<"请计算:"<<"a+b="<<endl;
cout<<"你的答案是:";
cin>>c;
if(c==s)
{
cout<<"Right!"<<endl;
n++;
}
else
cout<<"Not correct! "<<endl;
}
m=10-n;
k=10*n;
cout<<"做错的题数为:"<<m<<endl;
cout<<"总分为:"<<k<<endl;
}
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
int a,b,c,s,n=0,m,k,z;
for(int i=1;i<=10;i++)
cout<<"两个加数分别为:";
int j= rand();
int k= rand();
a=j%10;
b=k%10;
cout<<"a="<<a<<",b="<<b<<endl;
s=a+b;
cout<<"请计算:"<<"a+b="<<endl;
cout<<"你的答案是:";
cin>>c;
if(c==s)
{
cout<<"Right!"<<endl;
n++;
}
else
cout<<"Not correct! "<<endl;
}
}