gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 21:38:58 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com