using System;
using SystemCollectionsGeneric;
using SystemLinq;
using SystemText;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int howMany = 100;
int[] num = new int[howMany];
Random t = new Random();
ConsoleWriteLine(howMany + " random integers");
for (int i = 0; i < howMany; ++i)
{
num[i]=tNext();
}
for (int i = 0; i < howMany; i++)
{
ConsoleWrite(num[i] + "\t");
}
ConsoleWriteLine();
//求最大值
for (int i = 0; i < (howMany-1); i++)
{
if (num[i] > num[i+1])
{
int b = num[i];
num[i] = num[i + 1];
num[i + 1] = b;
}
}
ConsoleWriteLine("最大值为:" + num[howMany-1]);
//求最小值
for (int i = 0; i < (howMany - 1); i++)
{
if (num[i] < num[i + 1])
{
int b = num[i];
num[i] = num[i + 1];
num[i + 1] = b;
}
}
ConsoleWriteLine("最小值为:" + num[howMany - 1]);
//求平均值
double sum=0;
for (int i = 0; i < howMany; i++)
{
sum+=num[i];
}
sum = sum / howMany;
ConsoleWriteLine("平均值为:" + ConvertToInt32(sum));
ConsoleWriteLine();
ConsoleReadLine();
}
}
}
楼上可能出现的问题为SUM无法保证在int的范围内,而且ConsoleWrite("平均"+avg/100); 应改为ConsoleWrite("平均"+sum/100); ,效率楼上的高,学习了
using System;
using SystemCollectionsGeneric;
using SystemText;
namespace Conslx5 //这取决于你建立控制台程序时所取的名字;
{
class Program
{
static void Main(string[] args)
{
string [] a=new string [3]; //定义一个长度为4的一维数组,字符串型
string input;//定义用于接收输入的字数串
ConsoleWriteLine("请输入用逗号分隔的四个整数:");
input = ConsoleReadLine();
a = inputSplit(','); //将输入分开放入数组中
long max = ConvertToInt64(a[0]), min = ConvertToInt64(a[0]);
//定义了最大值,最小值,这里解释一下,long和int64指的是一个长度
//max ,min 必须在foreach循环外定义和初始化!!!
foreach (string aa in a)
{
if( ConvertToInt64(aa) >max){max=ConvertToInt64(aa);}
if (ConvertToInt64(aa) < min) { min = ConvertToInt64(aa); }
//这两行是比较每个值,把最大值放在max,最小值放在min里
}
ConsoleWriteLine("最大值是{0},最小值是{1}", max, min);
ConsoleReadKey();
}
}
}
C#代码,Linq直接有相关方法
using System;
using SystemCollectionsGeneric;
using SystemLinq;
List<double> list = new List<double>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
double max = listMax();
double min = listMin();
double average = listAverage();
using System;
using SystemCollectionsGeneric;
using SystemLinq;
using SystemText;
using SystemCollections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] array = new int[]{9,2,5,4,5,6,9,7,8,5,6};
int temp = 0;
int index = 0;
for (int i = 0; i < arrayLength;i++ )
{
if(temp < array[i]) //如果用<= 则找到的是最大值(多个中的最后一个) <则是多个中的第一个
{
temp = array[i];
index = i;
}
}
ConsoleWriteLine("最大值是{0},位置是{1}", temp, index + 1);
ConsoleReadKey();
}
}
}
以上就是关于在C#中如何求出最大值,最小值和平均值全部的内容,包括:在C#中如何求出最大值,最小值和平均值、C#语言中怎样实现输入4个整数,求出其最大值和最小值、编写一个函数,求n个数中的最大值,最小值,和平均值。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)