c++ - 如何在 C++ 中计算正确答案和错误答案?
问题描述
目前正在开发将循环直到用户输入“n”的添加程序。
它将生成两个随机数并显示给用户以添加它们。然后用户将输入答案和程序,并检查答案是对还是错。
我的代码运行良好,但是我需要以下代码的帮助来计算正确和错误的答案。
我什么都不累,因为我不知道该怎么做。
/******************************************************************************
Basic Template for our C++ Programs.
STRING
*******************************************************************************/
#include <stdio.h> /* printf, scanf, puts, NULL */
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include <string> // String managment funtions.
#include <iostream> // For input and output
#include <cmath> // For math functions.
#include <math.h>
#include <cstdlib>
using namespace std;
////////////////////////////////////////////////////////////////////////
int main()
{
srand(time(0));
string keepgoing;
do
{
const int minValue = 10;
const int maxValue = 20;
int y = (rand() % (maxValue - minValue + 1)) + minValue;
// cout<< " the random number is y "<< y << endl;
int x = (rand() % (maxValue - minValue + 1)) + minValue;
// cout<< " the random number is x "<< x << endl;
cout << " what is the sum of " << x << " + " << y << " =" << endl;
int answer;
cin >> answer;
if (answer == (x + y))
{
cout << "Great!! You are really smart!!" << endl;
}
else
{
cout << "You need to review your basic concepts of addition" << endl;
}
cout << "Do you want to try agian [enter y (yes) or n (no) ]";
cin >> keepgoing;
} while (keepgoing == "y");
return 0;
}
解决方案
为了让生活更轻松,让我们使用两个变量:
unsigned int quantity_wrong_answers = 0U;
unsigned int quantity_correct_answers = 0U;
(这应该在do
声明之前。)
当您检测到正确答案时,请增加以下变量之一:
if (answer = (x+y))
{
++quantity_correct_answers;
}
else
{
++quantity_wrong_answers;
}
在返回之前main
,您可以打印统计信息。
推荐阅读
- regex - 第 n 行的正则表达式结束
- cookies - 如何使用 urllib2 在 Python 中单独发送 cookie
- php - 在 Wordpress 简码中添加分类属性
- python - 有人可以向我解释为什么在等于 self.list 的列表上使用 pop() 也会让 self.list pop 排在最后
- sql - 尝试显示与表 3 相关的表 1 的 ID
- http - 如何使用自定义服务在 oauth 期间调试 Rocket Chat 错误?
- bash - 在 shell 脚本中使用带有完整路径名的二进制文件是否被认为是一种好习惯?
- php - preg_replace 在包含重复符号的字符串上
- oracle - 如何解决 Oracle DBMS_SCHEDULER.CREATE_JOB 问题?
- css - 如何将我的按钮设置为位于屏幕底部