C++任务协助

C++ Assignment Assistance

本文关键字:任务 C++      更新时间:2023-10-16

我必须创建一个嵌套的循环代码,要求用户输入弓箭手的分数,有 4 发子弹和 3 个弓箭手,所以每个回合程序都应该要求用户提示另外 3 个弓箭手的值。问题是它只是不断要求弓箭手得分,它不会进入下一轮,它甚至不会结束当前轮并显示该轮的平均分数。

#include <iostream>
using namespace std;
int main()
{
    //DECLARATIONS
    int score;
    int round;
    int total;
    double average = 0; // average score of an archer
    for (round = 0; round < 4;) {
        cout << "Please enter the Archer's Score' ";
        cin >> score;
        if (score<0, score> 60) {
            cout << "nThe value you entered is out of range, Please enter a number between 0 - 60 n";
        }
        total = total + score;
    }
    cout << "Total Score = " << total << endl;
    average = total / round;
    cout << "Average Score = " << average << endl;
    return 0;
}

这个怎么样? "for 循环"中的可变轮永远不会增加。

#include <iostream>
using namespace std;
int main()
{
    //DECLARATIONS
    int score;
    int round;
    int total;
    int count;
    double average = 0; // average score of an archer
    for (round = 0; round < 4; round++) {
        total = 0;
        for(count = 0; count < 3;)
        {
            cout << "Please enter the Archer's Score' ";
            cin >> score;
            if (score<0 || score> 60) {
                cout << "nThe value you entered is out of range, Please enter a number between 0 - 60 n";
            }
            else
            {
                count++;
                total = total + score;
            }
        }
        cout << "Total Score = " << total << endl;
        average = total / count;
        cout << "Average Score = " << average << endl;
    }
    return 0;
}

看看roundfor条件。您已经正确初始化了 round 变量,并为循环退出设置了正确的条件,但您的问题在于满足该条件,如何使 round 变量满足退出条件?

for (round = 0; round < 4;)

这个 for 循环是无限的,因为 round 变量永远不会递增。

if (score < 0 || score > 60) 

在这里,您正在生成错误消息,但不允许用户输入另一个分数来替换不正确的分数。