如何使用while循环来回答 [英] how to use while loop to answer

查看:115
本文介绍了如何使用while循环来回答的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写一个程序,询问用户班级中的学生人数。程序然后询问用户每个学生的成绩并计算/显示班级中的平均成绩。你需要使用while循环。





Write a program that asks the user for the number of students in the class. The program then asks the user for the grade of each student and computes/displays the average grade in the class. You are required to use a while loop.


#include <iostream>
using namespace std;
int main (){
	int studentNum;
	int sudentGrade;
	int avarageGrade;
	int countStudent = 0;
	cout << "Please enter the number of student = ";
	cin >> studentNum;
	while (studentNum>countStudent)
	{
		cout << "Please enter the grade = ";
		cin >> sudentGrade;

	}
	system("pause");
	return 0;
}

推荐答案

你应该总结所有学生的成绩。它可以在中逐步完成,而循环(在进入之前不要忘记初始化为 0 等级总和循环)。在循环结束时,通过将这个总和除以学生数来计算平均成绩值。
You should sum up the grades of all the students. It can be done incrementally, in the while loop (don't forget to initialize to 0 the grade sum before entering the loop). At the end of the loop compute the average grade value by dividing such sum by the number of the students.


这篇关于如何使用while循环来回答的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆