C# 如何获得c中的最大值#

C# 如何获得c中的最大值#,c#,C#,我有一个非常简单的C代码: static void Main(字符串[]args) { 整数i,pcm,最大值=0; 对于(i=1;i而言,这只是发布内容的另一种选择 static void Main(string[] args) { var numbers = new List<int>(); for (var i = 1; i <= 3; i++) { Console.WriteLine("Please enter your compute

我有一个非常简单的C代码:

static void Main(字符串[]args)
{
整数i,pcm,最大值=0;

对于(i=1;i而言,这只是发布内容的另一种选择

static void Main(string[] args)
{
   var numbers = new List<int>();

   for (var i = 1; i <= 3; i++)
   {
       Console.WriteLine("Please enter your computer marks");
       numbers.Add(int.Parse(Console.ReadLine()));
   }

   Console.WriteLine(string.Format("Maximum value: {0}", numbers.Max());
   Console.ReadKey();
}
static void Main(字符串[]args)
{
变量编号=新列表();

对于(var i=1;i而言,这只是发布的另一种选择

static void Main(string[] args)
{
   var numbers = new List<int>();

   for (var i = 1; i <= 3; i++)
   {
       Console.WriteLine("Please enter your computer marks");
       numbers.Add(int.Parse(Console.ReadLine()));
   }

   Console.WriteLine(string.Format("Maximum value: {0}", numbers.Max());
   Console.ReadKey();
}
static void Main(字符串[]args)
{
变量编号=新列表();
对于(var i=1;i只需跟踪它

对于每次迭代,如果输入的数字大于您在
maxm
中的数字,则将
maxm
设置为等于当前输入的数字

最后,您将获得最大值

伪代码:

max = 0
for three iterations
  get a number
  if that number is more than max
    then set max = that number
跟踪它就行了

对于每次迭代,如果输入的数字大于您在
maxm
中的数字,则将
maxm
设置为等于当前输入的数字

最后,您将获得最大值

伪代码:

max = 0
for three iterations
  get a number
  if that number is more than max
    then set max = that number

您可以将键入的值保存在
maxm
变量中。如果用户键入的数字较大,则替换该值:

static void Main(string[] args)
{
    int i, pcm = 0, maxm = 0;
    for (i = 1; i <= 3; i++)
    {
        Console.WriteLine("Please enter your computer marks");
        pcm = int.Parse(Console.ReadLine());

        // logic to save off the larger of the two (maxm or pcm)
        maxm = maxm > pcm ? maxm : pcm;
    }
    Console.WriteLine(string.Format("The max value is: {0}", maxm));
    Console.ReadKey();
}
static void Main(字符串[]args)
{
int i,pcm=0,maxm=0;
对于(i=1;i pcm?最大值:pcm;
}
WriteLine(string.Format(“最大值为:{0}”,maxm));
Console.ReadKey();
}

您可以将键入的值保存在
maxm
变量中。如果用户键入的数字较大,则替换该值:

static void Main(string[] args)
{
    int i, pcm = 0, maxm = 0;
    for (i = 1; i <= 3; i++)
    {
        Console.WriteLine("Please enter your computer marks");
        pcm = int.Parse(Console.ReadLine());

        // logic to save off the larger of the two (maxm or pcm)
        maxm = maxm > pcm ? maxm : pcm;
    }
    Console.WriteLine(string.Format("The max value is: {0}", maxm));
    Console.ReadKey();
}
static void Main(字符串[]args)
{
int i,pcm=0,maxm=0;
对于(i=1;i pcm?最大值:pcm;
}
WriteLine(string.Format(“最大值为:{0}”,maxm));
Console.ReadKey();
}
我想试试这个

static void Main(string[] args)
{
    int i, pcm, maxm = 0;
    for (i = 1; i <= 3; i++)
    {
        Console.WriteLine("Please enter your computer marks");
        pcm = int.Parse(Console.ReadLine());
        if(maxm <= pcm)
        {
             maxm = pcm;
        }
    }
    Console.ReadKey();
}
static void Main(字符串[]args)
{
整数i,pcm,最大值=0;
对于(i=1;i我会试试这个

static void Main(string[] args)
{
    int i, pcm, maxm = 0;
    for (i = 1; i <= 3; i++)
    {
        Console.WriteLine("Please enter your computer marks");
        pcm = int.Parse(Console.ReadLine());
        if(maxm <= pcm)
        {
             maxm = pcm;
        }
    }
    Console.ReadKey();
}
static void Main(字符串[]args)
{
整数i,pcm,最大值=0;

对于(i=1;i只是为了好玩,这里有另一个使用Linq的解决方案

static void Main(string[] args)
{
    int i, pcm, maxm = 0;
    List<int> vals = new List<int>();
    for (i = 1; i <= 3; i++)
    {
    Console.WriteLine("Please enter your computer marks");
    pcm = int.Parse(Console.ReadLine());
    vals.Add(pcm);
    }
    maxm = vals.Max(a => a);
    Console.ReadKey();
}
static void Main(字符串[]args)
{
整数i,pcm,最大值=0;
List VAL=新列表();
对于(i=1;i a);
Console.ReadKey();
}

为了好玩,这里有另一个使用Linq的解决方案

static void Main(string[] args)
{
    int i, pcm, maxm = 0;
    List<int> vals = new List<int>();
    for (i = 1; i <= 3; i++)
    {
    Console.WriteLine("Please enter your computer marks");
    pcm = int.Parse(Console.ReadLine());
    vals.Add(pcm);
    }
    maxm = vals.Max(a => a);
    Console.ReadKey();
}
static void Main(字符串[]args)
{
整数i,pcm,最大值=0;
List VAL=新列表();
对于(i=1;i a);
Console.ReadKey();
}

在这里解释(没有复制/粘贴代码)可能是一个更好的选择。它看起来像一个家庭作业问题。(不是向下投票;只是评论。)在这里解释(没有复制/粘贴代码)可能是一个更好的选择。它看起来像一个家庭作业问题。(不是向下投票;只是评论。)+1用于清晰解释和伪代码,而不仅仅是键入代码解决方案。很好地利用了教育机会。:-+1用于清晰解释和伪代码,而不仅仅是键入代码解决方案。很好地利用了教育机会。:-)