1 . 编写控制台应用程序,用户任意输入n个整数,程序计算并输出最大值、最小值、中位数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/*
* 编写控制台应用程序,用户任意输入n个整数,程序计算并输出最大值、最小值、中位数和平均值。
*/
namespace sxl_1
{
class Program
{
static void Main(string[] args)
{
// int min ;
// int max ;
double middle;
double average;
int n;
String s;
Console.WriteLine("欢迎使用本程序");
Console.WriteLine("================================");
Console.WriteLine("请输入您之后想输入的整数的个数");
s = Console.ReadLine();
n = Convert.ToInt32(s);
Console.WriteLine("请输入想要判断的数,注意用空格相间");
s = Console.ReadLine();
string[] str = s.Split(' ');
int i, j;
for (i = 0; i < n; i++)
for (j = i + 1; j < n; j++)
{
if (Convert.ToInt32(str[i]) > Convert.ToInt32(str[j]))
{
String temp;
temp = str[i];
str[i] = str[j];
str[j] = temp ;
}
}
int sum = 0;//局部变量使用前要先赋值,不然会报错
for(i = 0; i < n; i++)
{
sum = sum + Convert.ToInt32(str[i]);
}
average = sum / n;
if( n % 2 == 0)
{
middle = ( Convert.ToInt32(str[n/2])+Convert.ToInt32(str[n/2+1]))/2;
}
else
middle = Convert.ToInt32(str[n/2]);
Console.WriteLine("最大数 : " + str[n-1]);
Console.WriteLine("最小数 : " + str[0]);
Console.WriteLine("平均数 : " + average );
Console.WriteLine("中位数 : " + middle);
}
}
}