生成加法题

来源:互联网 发布:数据挖掘以什么为基础 编辑:程序博客网 时间:2024/06/07 03:24

使用rand()生成三个伪随机数,输出成一道加法题,然后验证结果。
使用while(1)一直循环,直到if(…)break;

#include<iostream>#include<string>#include<ctime>#include<cstdlib>using namespace std;int main(void){while(1){    srand(time(0));    int number1=rand() %100;    int number2=rand() %100;    int number3=rand() %100;    cout<<number3<<"+"<<number2<<"+"<<number1<<"= ?"<<endl;    int sum=number3+number2+number1;    cout<<"the anwser is :";    int anwser=0;    cin>>anwser;    bool isCorrect=(anwser==sum);    if(isCorrect)        {    cout<<"Congradulation!You are correct"<<endl;        }    else    {    cout<<"Sorry,you are wrong!"<<endl;    }       cout<<"Do you want to do it again?[yes/no]:"<<endl;    string reslut;//错误 string reslut=0 将NULL赋值给std::string变量     cin>>reslut;    if(reslut=="no")    break;  }return 0;}
0 0
原创粉丝点击