gpt4 book ai didi

c++ - 初学者 C++ 程序中的分数组件

转载 作者:行者123 更新时间:2023-11-28 03:00:41 24 4
gpt4 key购买 nike

我正在做一项作业,我应该编写一个程序来测试用户的数学技能。这是我现在拥有的代码:

using namespace std;
void addition()
{
int Value, Value2, Answer;
bool gotAnswer;

for (int i=1; i<=10; i++)
{
Value = 1 + (rand()%10);
Value2 = 1 + (rand()%10);

gotAnswer = false;
cout << Value << " + " << Value2 << " = ";

for (int a=1; (a<=3 && gotAnswer==false); a++)
{
cin >> Answer;
if (Answer==(Value+Value2))
{
cout << "CORRECT" << endl << endl;
gotAnswer = true;
}
else
{
cout << "WRONG Try again." << endl;
if (a==3)
{
cout << "You have missed 3 times. The answer is " << Value+Value2 << "." << endl << endl;
}
}
}
}
}

void substraction()
{
int Value, Value2, Answer;
bool gotAnswer;

for (int i=1; i<=10; i++)
{
Value = 1 + (rand()%10);
Value2 = 1 + (rand()%10);

gotAnswer = false;
cout << Value << " - " << Value2 << " = ";

for (int a=1; (a<=3 && gotAnswer==false); a++)
{
cin >> Answer;
if (Answer==(Value-Value2))
{
cout << "CORRECT" << endl << endl;
gotAnswer = true;
}
else
{
cout << "WRONG Try again." << endl;
if (a==3)
{
cout << "You have missed 3 times. The answer is " << Value-Value2 << "." << endl << endl;
}
}
}
}
}

void Multiplication()
{
int Value, Value2, Answer;
bool gotAnswer;

for (int i=1; i<=10; i++)
{
Value = 1 + (rand()%10);
Value2 = 1 + (rand()%10);

gotAnswer = false;
cout << Value << " x " << Value2 << " = ";

for (int a=1; (a<=3 && gotAnswer==false); a++)
{
cin >> Answer;

if (Answer==(Value*Value2))
{
cout << "CORRECT" << endl << endl;
gotAnswer = true;
}
else
{
cout << "WRONG Try again." << endl;
if (a==3)
{
cout << "You have missed 3 times. The answer is " << Value*Value2 << "." << endl << endl;
}
}
}
}
}

int main()
{
int number;

cout << "Enter the number for the problem type desired:"<<endl;
cout << " 1. Addition"<<endl;
cout << " 2. Subtraction"<<endl;
cout << " 3. Multiplication"<<endl;

cin >> number;

if (number == 1)
{
addition();
}
else if(number == 2)
{
substraction();
}
else if (number ==3)
{
Multiplication();
}
}

程序运行良好。但是,应该有一个分数组件,用户在第一次尝试时获得 10 分,第二次尝试获得 5 分,第三次尝试/错误时获得 0 分。我不知道如何在 10 个问题的末尾混合分数组件和显示。请提示?

提前致谢。

最佳答案

您应该在每个函数中保留一个分数变量,根据需要添加到分数变量,然后返回分数变量。

所以这些函数不再是 void,它们将是 int。然后你可以在最后得到分数并打印出来。

我不会为你写任何代码,因为它是一个作业:P

关于c++ - 初学者 C++ 程序中的分数组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20929835/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com