计算平均值,不包括上次得分
Calculate the average without including last score
如果不包括最后一场比赛,我如何计算平均值。我不想包括最后一个游戏,因为它结束了用户需要输入-1的循环。所以,当用户输入-1时,这个游戏被包括在平均值中,而不应该这样结束游戏,而不是实际的分数。有办法绕过这个吗?
while (points != -1)
{
total = total + points;
game++;
cout << "Enter the points for game " << game << ": ";
cin >> points;
average = total / game;
}
cout << "nThe total points are " << total << endl;
cout << "n The average points are " << average << endl;
system("PAUSE");
return 0;
}
部分基于描述和缺失的代码,很难准确地说出您想要什么。我假设-1表示"停止循环">
以下是我认为您正在寻找的:
game = 0;
total = 0;
while (1) {
++game;
cout << "Enter the points for game " << game << ": ";
cin >> points;
if (points == -1)
break;
total = total + points;
}
game -= 1;
if (game > 0)
average = total / game;
else
average = 0;
cout << "nThe total points are " << total << endl;
cout << "n The average points are " << average << endl;
system("PAUSE");
return 0;
如果是-1,你可以在除以总分和减量之前测试你的分数:
while (points != -1)
{
total = total + points;
game++;
cout << "Enter the points for game " << game << ": ";
cin >> points;
if(points==-1){
game--;}
average = total / game;
}
cout << "nThe total points are " << total << endl;
cout << "n The average points are " << average << endl;
system("PAUSE");
return 0;
}
while (points != -1) // <--3
{
total = total + points;
game++;
cout << "Enter the points for game " << game << ": "; // <--1
cin >> points;
average = total / game; // <--2
}
我标记了操作顺序。问题是,在检查了"-1"之后,您要添加要求平均值的点。
while (temp != -1)
{
total = total + points;
cout << "Enter the points for game " << game << ": ";
cin >> temp;
if(temp != -1)
{
game++;
points = temp;
average = total / game;
}
}
我添加了一个变量来临时保存要检查的输入值,然后再修改要求平均值的主变量。
相关文章:
- 为什么 cmake 许可证<>样式不包括?
- 计算平均值,不包括上次得分
- 将值从另一个数组写入数组,不包括不需要的值 C++
- 如果语句不包括文本行?
- 找到 x^n 的所有组合,并检查它们的总和是否等于一个不包括相同数字的数字
- 如何保证不包括 iostream?
- 创建一个循环,该循环将输出大于零且小于 60(不包括 60)的所有 5 的倍数
- C/C++ 经过的流程周期,不包括断点处
- 回文,不包括特殊字符和空格
- 查找 n 个数字的公因数数(不包括"1"作为公因数)
- C 平均程序在循环时不计算n后不计算平均值
- 在 C++ 中的 SetClipboardData() 之后粘贴不包括记事本的换行符
- C++11 exit() 和 abs() 不包括 <cstdlib>?
- 生成生成 makefile 时不包括 cmake C++11 标志
- 寻找一种单一的定时方法来测试各种算法,不包括它们的输入
- 提升正则表达式 cpp 用于查找 %% 之间的字符串,输出不包括 % 字符本身
- gSoap 不包括信封和正文开始标签
- 与不包括一个标头更好的使用不完整的标头比另一个标头的使用无效
- 在不包括任何内容的情况下获得错误声音
- 运行单元测试不包括Catch2中的特定标签