C++ 重复向a询问用户等级,并使用C+存储数据+;?
我必须编写一个程序,无限期地要求用户提供0到100之间的分数,一旦用户输入数字“-1”,它将获取所有数据,并给出最高分数和平均值 我很难让它重复这个问题,我很困惑,当数据完全随机时,如何存储和计算所有这些数据 这就是我到目前为止想到的C++ 重复向a询问用户等级,并使用C+存储数据+;?,c++,C++,我必须编写一个程序,无限期地要求用户提供0到100之间的分数,一旦用户输入数字“-1”,它将获取所有数据,并给出最高分数和平均值 我很难让它重复这个问题,我很困惑,当数据完全随机时,如何存储和计算所有这些数据 这就是我到目前为止想到的 #include <iostream> using namespace std; int main(){ int num; cout<<"To get final and highest score, enter -1."&
#include <iostream>
using namespace std;
int main(){
int num;
cout<<"To get final and highest score, enter -1."<<endl;
cout<<"Enter the score for student 1: ";
cin>>num;
while (num < 0 || num > 100){
cout<<"Wrong. You must enter a number between 0 to 100.\n";
cin>>num;
}
if (true){
cout<<"Enter the grade for student 2: ";
cin>>num;
}
return 0;
}
#包括
使用名称空间std;
int main(){
int-num;
cout一些伪代码:
main(){
nMax;
avg;
end = false;
num;
nNums = 0;
cout << "to exit enter -1";
while(!end){
do{
cout << "Enter number";
cin >> num;
}while(num < -2 || num > 100);
if ( num < 0){
end = true;
}else{
avg = ((avg * nNums) + num) / ++nNums;
nMax = max(nMax, num);
}
}
cout << avg << ", " << nMax;
}
main(){
nMax;
平均值;
结束=假;
号码;
nNums=0;
库特数;
}而(num<-2 | | num>100);
if(num<0){
结束=真;
}否则{
平均值=((平均值*nNums)+num)/++nNums;
nMax=最大值(nMax,num);
}
}
cout给你:
#include <iostream>
int main(){
int num;
int sum=0, count=0;
int maxGrade = -1;
std::cout<<"To get final and highest score, enter -1."<<std::endl;
while(true) {
std::cout<<"Enter the score for student "<<count+1<<":"<<std::endl;
std::cin>>num;
if(num == -1) break; // <<<< test specifically for break condition
if(num < 0 || num > 100) { // <<<< test for any other invalid input
std::cout<<"Invalid grade. You must enter a number between 0 and 100.\n";
}
else {
count++; // <<<< track number of valid inputs
sum += num; // <<<< track total grade
if(num>maxGrade) maxGrade = num; // <<<< track highest grade so far
}
}
std::cout<<"Number of students: "<<count<<std::endl;
std::cout<<"Average is "<< sum * 1.0 / count<<std::endl;
std::cout<<"Highest grade is "<<maxGrade<<std::endl;
return 0;
}
要计算最高等级和平均值,不需要存储所有输入的值。你所需要做的就是计算输入的数字之和,它的数量和最大等级。该程序可以如下所示
#include <iostream>
using namespace std;
int main()
{
bool empty = true;
int sum = 0;
int cnt = 0;
int max;
cout << "To get final and highest score, enter -1." << endl;
while ( true )
{
cout << "Enter the score for student " << n + 1 << ": ";
int num = -1;
cin >> num;
if ( num == -1 ) break;
if ( num < 0 || num > 100 )
{
cout << "Wrong. You must enter a number between 0 to 100.\n";
}
else
{
if ( empty || max < num ) max = num;
empty = false;
sum += num;
++n;
}
}
if ( !empty )
{
cout << "The maximum score is " << max << endl;
cout << "The average score is " << sum / n << endl;
// cout << "The average score is " << ( sum + 0.0 ) / n << andl; //that to get a float number
}
return 0;
}
#包括
使用名称空间std;
int main()
{
bool empty=true;
整数和=0;
int-cnt=0;
int max;
难道你不需要存储“所有的数据”-只有最高的值…你可以计算条目的总和和数量来跟踪平均值。这比保留所有的数字更干净。当数字<0时,你必须打破循环,而不是说“错”。这样你就永远出不去了…if(true){
显然是多余的!我该如何计算条目的总和和数量?大多数std::endl应该被替换为“\n”,您正在无需任何需要地刷新流。@Klaim:我认为“应该被替换”在这种情况下有点强。我不认为像这样的交互式程序对性能至关重要。“可能被替换”可能是一个更好的术语。我认为这是一个偏好问题。例如,请参阅讨论。这确实指出,cin
将刷新输出缓冲区-因此确实不需要显式执行。
#include <iostream>
using namespace std;
int main()
{
bool empty = true;
int sum = 0;
int cnt = 0;
int max;
cout << "To get final and highest score, enter -1." << endl;
while ( true )
{
cout << "Enter the score for student " << n + 1 << ": ";
int num = -1;
cin >> num;
if ( num == -1 ) break;
if ( num < 0 || num > 100 )
{
cout << "Wrong. You must enter a number between 0 to 100.\n";
}
else
{
if ( empty || max < num ) max = num;
empty = false;
sum += num;
++n;
}
}
if ( !empty )
{
cout << "The maximum score is " << max << endl;
cout << "The average score is " << sum / n << endl;
// cout << "The average score is " << ( sum + 0.0 ) / n << andl; //that to get a float number
}
return 0;
}