【发布时间】:2021-11-11 18:01:01
【问题描述】:
我有一个程序可以找到最低和最高等级。最大值函数有效,但最小值函数返回 0。我不确定我做错了什么,我的代码中没有错误。谢谢!
使用系统; 使用 System.Collections.Generic;
命名空间等级转换器 { 课堂节目 { 静态无效主要(字符串 [] 参数) { bool doAgain = true; //float totalScore = 0 , averageScore; 字符串更多转换; int numberOfGrades;
//Ask the user to enter their first and last name
string userName = getName();
//Print a welcome message: "Hello [first name] [last name] Welcome to the Grade Converter!"
Console.WriteLine($"\nHello {userName}! Welcome to the Grade Converter!");
while(doAgain){
List<float> scores = new List<float>();
//Prompt the user to enter the number of grades they need to convert "Enter the number of grades you need to convert: "
numberOfGrades = getNumberOfGrades();
//Prompt the user to enter the grades. The grades should be stored in a list and you should use the appropriate data type for the grades.
scores = populateGradesList(scores, numberOfGrades);
//Print Grade Report
printGradeReport(scores);
printGradeStatistics(scores, numberOfGrades);
float aveScore = getAverageGrade(scores);
float maximumGrade = getMaximumGrade(scores);
float minimumGrade = getMinimumGrade(scores);
//averageScore = totalScore / numberOfGrades;
Console.WriteLine("Would you like to convert more grades (y/n)?");
moreConversions = Console.ReadLine();
//reset total score
//totalScore = 0;
if (moreConversions != "y" && moreConversions != "Y"){
doAgain = false;
}
}
}
static float getMinimumGrade(List<float> listOfNumberGrades){
float min = 0;;
foreach(float grade in listOfNumberGrades){
if( grade < min){
min = grade;
}
}
return min;
}
static float getMaximumGrade(List<float> listOfNumGrades){
float max = 0;
foreach(float grade in listOfNumGrades){
if( grade > max){
max = grade;
}
}
return max;
}
static float getAverageGrade(List<float> listOfGrades){
float total = 0;
float average = 0;
foreach(float grade in listOfGrades){
total += grade;
}
average = total / listOfGrades.Count;
return average;
}
static void printGradeStatistics(List<float> listOfGrades, int numberOfGrades){
float averageScore = getAverageGrade(listOfGrades);
float maximumGrade = getMaximumGrade(listOfGrades);
float minimumGrade = getMinimumGrade(listOfGrades);
Console.WriteLine("\nGrade Statistics\n-------------------------\n");
Console.WriteLine($"Number of grades: {numberOfGrades}");
Console.WriteLine($"Average Grade: {averageScore} which is a {getLetterGrade(averageScore)}");
Console.WriteLine($"Maximum Grade: {maximumGrade}");
Console.WriteLine($"Minimum Grade: {minimumGrade}");
}
static void printGradeReport(List<float> scoresList){
string letterGrade;
Console.WriteLine();
foreach(float testScore in scoresList){
//Convert the number grades to letter grades (A = 90-100, B = 80-89, C = 70-79, D = 60 - 69, F lower than 60)
letterGrade = getLetterGrade(testScore);
//Print all the numerical grades and their corresponding letter grades to the console
Console.WriteLine($"A score of {testScore} is a {letterGrade} grade");
}
return;
}
static List<float> populateGradesList(List<float> listOfScores, int numGrades){
float score;
for(int counter = 0; counter < numGrades; counter ++){
score = getScores();
listOfScores.Add(score);
}
return listOfScores;
}
static int getNumberOfGrades(){
Console.WriteLine("\nEnter the number of grades you need to convert:");
int numberOfGrades = int.Parse(Console.ReadLine());
return numberOfGrades;
}
static string getName(){
Console.WriteLine("Please enter your first name and last name");
string userName = Console.ReadLine();
return userName;
}
static float getScores(){
float grade;
while(true){
Console.WriteLine("\nEnter the score to be converted: ");
try{
grade = float.Parse(Console.ReadLine());
break;
}catch(FormatException){
Console.WriteLine("Error: Only numbers allowed");
}
}
return grade;
}
static string getLetterGrade(float score){
//(A = 90-100, B = 80-89, C = 70-79, D = 60 - 69, F lower than 60).
if (score >= 90){
return "A";
}else if(score >= 80){
return "B";
}else if (score >= 70){
return "C";
}else if(score >= 60){
return "D";
}else{
return "F";
}
}
}
}
【问题讨论】:
-
getMinimumGrade 首先将最小值设置为 0。您需要做的是将起始最小值设置为集合中的第一个。
-
我怎么能做到这一点? @尼尔