1个回答
展开全部
using System;
namespace ConsoleApplication3
{
class Test
{
public void GetMax(int[] a, out int max)
{
max = int.MinValue;
for (int i = 0; i < a.Length; i++)
{
if (a[i] > max) max = a[i];
}
}
}
class Program
{
static void Main(string[] args)
{
int[] a = { 1, 100, 2, 3, 4 };
int m = 0;
Test t = new Test();
t.GetMax(a, out m);
Console.WriteLine("Max = {0}", m);
}
}
}
Test类实现你要求的功能!
追问
最小值也是这样 ?
追答
using System;
namespace ConsoleApplication3
{
class Test
{
public void GetMax(int[] a, out int max, out int min)
{
max = int.MinValue;
min = int.MaxValue;
for (int i = 0; i < a.Length; i++)
{
if (a[i] > max) max = a[i];
if (a[i] < min) min = a[i];
}
}
}
class Program
{
static void Main(string[] args)
{
int[] a = { 1, 100, 2, 3, 4 };
int max = 0;
int min = 0;
Test t = new Test();
t.GetMax(a, out max, out min);
Console.WriteLine("Max = {0}; Min = {1}", max, min);
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询