求一道编程题目的答案(是C++的) 财富值39

2017-04-04 19:38发布

给学生出一道加法运算题,然后判断学生输入的答案对错与否,按下列要求以循序渐进的方式编程。程序1 通过输入两个加数给学生出一道加法运算题,如果输入答案正确,则显示“Right!”,否则显示“Not correct! Try again!”,程序结束。程序2 通过输入两个加数给学生出一道加法运算题,如果输入答案正确,则显示“Right!”,否则显示“Not correct! Try again!”,直到做对为止。程序3 通过输入两个加数给学生出一道加法运算题,如果输入答案正确,则显示“Right!”,否则提示重做,显示“Not correct! Try again!”,最多给三次机会,如果三次仍未做对,则显示“Not correct! You have tried three times! Test over!”,程序结束。程序4 连续做10道题,通过计算机随机产生两个1~10之间的加数给学生出一道加法运算题,如果输入答案正确,则显示“Right!”,否则显示“Not correct!”,不给机会重做,10道题做完后,按每题10分统计总得分,然后打印出总分和做错的题数。
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
1条回答
开辆拖拉机
2020-12-30 17:43

程序代码如下:

第一个程序

#include <iostream>
using namespace std;
int main ()
{
 float a, b, sum;
 cout << "请输入加数a:";
 cin >> a;
 cout << "请输入加数b:";
 cin >> b;
 cout << "\n请输入结果\na+b=";
 cin >> sum;
 if (sum == (a+b))
  cout << "Right!\n" << endl;
 else
  cout << "Not correct! Try again!" << endl;
 return 0;
}

第二个程序

#include <iostream>
using namespace std;
int main ()
{
 float a, b, sum;
 bool flag = false;
 cout << "请输入加数a:";
 cin >> a;
 cout << "请输入加数b:";
 cin >> b;
 while (!flag)
 {
  cout << "\n请输入结果\na+b=";
  cin >> sum;
  if (sum == (a+b))
  {
   flag = true;
   cout << "Right!\n" << endl;
  }
  else
  {
   cout << "Not correct! Try again!" << endl;
  }
 }
 return 0;
}

第三个程序

#include <iostream>
using namespace std;
int main ()
{
 float a, b, sum;
 int i = 0;
 bool flag = false;
 cout << "请输入加数a:";
 cin >> a;
 cout << "请输入加数b:";
 cin >> b;
 while (!flag && (i < 3))
 {
  cout << "\n请输入结果\na+b=";
  cin >> sum;
  if (sum == (a+b))
  {
   flag = true;
   cout << "Right!\n" << endl;
  }
  else
  {
   cout << "Not correct! Try again!" << endl;
  }
  i++;
 } if (i == 3)
 {
  cout << "Not correct! You have tried three times! Test over!" << endl << endl;
 }
 return 0;
}

第四个程序

#include <iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main ()
{
 float a, b, sum, count = 0, score = 0;
 int i = 0;
 srand ((unsigned)time (NULL));  //产生随机数种子
 while (i < 10)
 {
  //a+(int)(n*rand()/(RAND_MAX+1.0))产生一个a到n之间的随机数
  
  a = 1 + (int) (10 * rand () / (RAND_MAX + 1));  b = 1 + (int) (10 * rand () / (RAND_MAX + 1));  cout << "\n请输入结果\n" << a << "+" << b << "=";
  cin >> sum;
  if (sum == (a+b))
  {
   score += 10;
   cout << "Right!\n" << endl;
  }
  else
  {
   count++;
   cout << "Not correct! Try again!" << endl;
  }
  i++;
 }
 
 cout << "得分:" << score << "\t\t做错了" << count << "道题" << endl;
 return 0;
}


一周热门 更多>