我的代码要求用户输入一定数量的学生,并根据学生的数量输入姓名和分数。我的问题是,如何使用 if 语句找出得分最高的学生。 (C#)
class Class1
{
public void studentCount()
{
int studentcount;
int i;
int x;
string studentname = "";
Console.WriteLine("Enter how many students there are: ");
studentcount = Convert.ToInt32(Console.ReadLine());
{
for (i = 1; i <= studentcount; i++)
{
Console.WriteLine("Enter students name: ");
studentname = Convert.ToString(Console.ReadLine());
Console.WriteLine("Enter students score: ");
x = Convert.ToInt32(Console.ReadLine());
}
}
最佳答案
跟踪当前最高 x(学生的分数),如果输入的新分数高于最后一个最高分数,则更新最后一个最高分数的变量和该学生的姓名。为此,您需要再使用一个变量来跟踪当前的最高分数。您可以随时更新学生的姓名,以匹配最高分的学生。循环结束后,您可以输出该学生的分数和姓名。
关于c# - 使用 if 语句确定最高值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20789464/