初学者C++程序中的分数部分

Score component in beginner C++ program

本文关键字:数部 C++ 程序 初学者      更新时间:2023-10-16

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

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 个问题的末尾显示。请提示一下?

提前感谢。

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

所以这些函数不再是空的,它们将是整数。然后,您可以在最后获得分数并将其打印出来。

我不会为你编写任何代码,因为它是用于赋值:P