c#写一个类Score,为该类编写3个方法,其参数均为整型数组。 其中2个静态函数(static),分别命名为GetMax和GetMin, 用于计算指定数组中的最大、最小值; 另1个为实例方法(无static),命名为GetAverage,用于计算指定数组的平均值。 然后随机生成一个有3个元素的数组,写出计算最小值和平均数的调用代码。(Main方法)
时间: 2024-03-14 12:46:46 浏览: 26
好的,下面是代码实现:
```csharp
using System;
class Score
{
public double GetAverage(int[] scores)
{
double sum = 0;
foreach (int score in scores)
{
sum += score;
}
return sum / scores.Length;
}
public static int GetMax(int[] scores)
{
int max = int.MinValue;
foreach (int score in scores)
{
if (score > max)
{
max = score;
}
}
return max;
}
public static int GetMin(int[] scores)
{
int min = int.MaxValue;
foreach (int score in scores)
{
if (score < min)
{
min = score;
}
}
return min;
}
}
class Program
{
static void Main(string[] args)
{
int[] scores = { 85, 92, 78 };
Console.WriteLine($"最小值为:{Score.GetMin(scores)}");
Console.WriteLine($"平均数为:{new Score().GetAverage(scores)}");
}
}
```
运行结果:
```
最小值为:78
平均数为:85
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)