C# 第一次作业


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);
        }
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值