我正在制作一个控制台程序,你必须说明课堂上有多少学生,以及你在这门课上做了多少次考试,所以它最后会给你一个学生名字。最终得分。 问题在于我无法进行最终的微积分=(sum / numberOfTests)。它只取你告诉程序的最后一个年级。这是代码。
namespace Repeat
{
class Program
{
static void Main(string[] args)
{
//VARIABLES
float numberOfStudents;
float numberOfTests;
float average = 0;
float grade;
float sum = 0;
string studentName;
Console.WriteLine("Type the number of students");
float.TryParse(Console.ReadLine(), out numberOfStudents);
Console.WriteLine("Type the number of tests");
float.TryParse(Console.ReadLine(), out numberOfTests);
Console.WriteLine("");
for (int i = 1; i <= numberOfStudents; i++)
{
Console.Write("Student name {0}: ", i);
studentName = Console.ReadLine();
for (int p = 1; p <= numberOfTests; p++)
{
Console.Write("Test {0} grade: ", p);
float.TryParse(Console.ReadLine(), out grade);
average = sum / numberOfTests;
}
if (average < 7)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Average: " + average);
Console.ResetColor();
}
else if (average >= 7)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Average: " + average);
Console.ResetColor();
}
Console.WriteLine("");
}
///END
Console.ReadKey();
}
}
}
答案 0 :(得分:0)
实际上你没有设置和更新sum
值,所以总是0
,像这样修复你的内部for循环
sum = 0;
for (int p = 1; p <= numberOfTests; p++)
{
Console.Write("Test {0} grade: ", p);
float.TryParse(Console.ReadLine(), out grade);
sum = sum + grade;
average = sum / numberOfTests;
}