c# - 使用 if 语句确定最高值

标签 c# if-statement

我的代码要求用户输入一定数量的学生,并根据学生的数量输入姓名和分数。我的问题是,如何使用 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/

相关文章:

c# - NSUserDefaults-Xamarin

c# - cosmos 中的创建/查询抛出取消异常

c - 需要左值作为赋值的左操作数 - 错误? : in C

iphone - 在 else if 条件下迭代一个变量?

c# - Unity中的Encog dll?

c# - 将 Action 委托(delegate)作为参数传递给构造函数时出现参数错误

c# - 如何解释 Math.Acos() 报告无效输入的情况?

java - 将重复的 if-else 语句修改为更清晰的代码

C++ 如果在函数中不起作用

oracle - 使用 if 语句 PL/SQL 更新表