C++ 代码:100 以内加减运算练习
以下是使用 C++ 编写的 100 以内加减运算练习代码:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int a, b, c, answer, choice;
srand(time(0));
cout << '请选择题目类型:1.加法 2.减法' << endl;
cin >> choice;
if (choice == 1)
{
a = rand() % 100 + 1;
b = rand() % 100 + 1;
cout << a << ' + ' << b << ' = ';
cin >> answer;
c = a + b;
if (answer == c)
cout << '回答正确!' << endl;
else
cout << '回答错误!正确答案为' << c << endl;
}
else if (choice == 2)
{
a = rand() % 100 + 1;
b = rand() % 100 + 1;
cout << a << ' - ' << b << ' = ';
cin >> answer;
c = a - b;
if (answer == c)
cout << '回答正确!' << endl;
else
cout << '回答错误!正确答案为' << c << endl;
}
else
{
cout << '输入错误!' << endl;
}
return 0;
}
说明:
这段代码可以让用户选择加法或减法题目类型,然后自动生成两个随机数,让用户计算出结果,并进行判断回答是否正确。
原文地址: https://www.cveoy.top/t/topic/n7K0 著作权归作者所有。请勿转载和采集!