我有一个程序,我正在努力练习期中考试,这是一个简单的成绩簿计划,允许用户输入0-100的任意数量的成绩。我有各种各样的东西,最终计算工作正常,但我需要该程序也显示最低和最高等级。我看到另一个帖子,其中有人谈到linq(?)我没有使用过这个,还没有研究过它。我想如果书中没有它我还不需要它。代码很长,所以我会尝试只包括我认为必要的内容......
using System;
public class GradeBook
{
private int total;
private int gradeCounter;
private int aCount;
private int bCount;
private int cCount;
private int dCount;
private int fCount;
// automatic popert CourseName
public string CourseName { get; set; }
然后才能获得用户输入
public void InputGrades()
{
int grade; //grade made by user
string input; //text entered by user
Console.WriteLine("{0}\n{1}",
"enter integer in the range of 0-100.",
"Type <ctrl> Z and press endter to terminate input:");
input = Console.ReadLine(); //user input
//loop until user enters the end of file indicator
while (input != null)
{
grade = Convert.ToInt32(input);
total += grade; //create total to form sum
++gradeCounter;//increment the number of grades for average
// call method to increment appropriate counter
IncrementLetterGradeCounter(grade);
input = Console.ReadLine();
}//end while(input != null)
这是我发给SO的第一篇文章,所以如果我做错了请告诉我。如果需要更多代码,请告诉我。
答案 0 :(得分:2)
在int等级
下添加这两个int high = 0;
int low = 100;
然后在你的while循环中
if(input > high) high = input;
if(input < low) low = input;
然后在你的while循环之后。
Console.WriteLine("The highest grade was: " + high);
Console.WriteLine("The lowest grade was: " + low);