C++任务协助
C++ Assignment Assistance
我必须创建一个嵌套的循环代码,要求用户输入弓箭手的分数,有 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;
}
看看round
和for
条件。您已经正确初始化了 round 变量,并为循环退出设置了正确的条件,但您的问题在于满足该条件,如何使 round 变量满足退出条件?
for (round = 0; round < 4;)
这个 for 循环是无限的,因为 round 变量永远不会递增。
if (score < 0 || score > 60)
在这里,您正在生成错误消息,但不允许用户输入另一个分数来替换不正确的分数。
相关文章:
- 有没有任务栏API可以立即应用注册表更改
- 如何创建线程序列以按照启动顺序执行任务?
- C++一个线程如何正确通信其任务已完成?
- 在线程之间拆分任务总是值得的吗?
- 递归求和任务的错误答案
- "main"函数堆栈中的对象在第一个任务运行时被覆盖 (FreeRTOS)
- C++ 任务流库入门
- Pisarze - 来自波兰奥林匹克信息学的数据分析任务
- 在程序运行时监视 VxWorks 中的任务 CPU 利用率
- 编写一个读取五个整数并执行一些任务的C++程序
- 任务内部的 OpenMP 任务循环
- 不知道如何在家庭作业任务中实现一件事
- 如何安全地停止 IOCP WSARecv() 任务,并释放 WSAOVERLAPED 结构?
- 为什么我的 FreeRTOS 任务看不到类成员?
- 当前步骤:构建任务.json 文件
- 命令模式 - 使用"weight"执行任务的命令
- 无法删除EXE崩溃后,即使进程未显示在任务管理器中
- 需要帮助来理解我的任务的 QRegularExpression
- xSemaphoreTake 在调用 xSemaphoreGive 后不会恢复任务
- 如果互斥锁被锁定,则安全地跳过任务