C#练习题18和19和20和21

第18题:
从一个整数数组中取出最大的整数、最小的整数、总和、平均数

using System;

namespace 练习题18
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8 };
            int max = 0, min = 0, sum = 0;
            double average = 0.0;
            for(int i=0;i<nums.Length;i++)
            {
                if (nums[i] >= max)
                    max = nums[i];
                else
                    min = nums[i];
                sum += nums[i];
                average = ((double)sum) / nums.Length;
            }
            Console.WriteLine("该数组中最大的整数是:{0},最小整数是:{1},总和是:{2},平均数是:{3}",max,min,sum,average);
            Console.ReadKey();
        }
    }
}

在这里插入图片描述

第19题:
数组里面都是人的名字,分割成:例如:老杨|老苏|老邹…“(老杨,老苏,老邹,老虎,老牛,老蒋,老王,老马)

using System;

namespace 练习题19
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] names = { "老杨", "老苏", "老邹", "老虎", "老牛", "老蒋", "老王", "老马" }; 
            string str = null;
            for(int i=0;i<names.Length-1;i++)
            {
                str += names[i] + "|";
            }
            str = str + names[names.Length - 1];
            Console.WriteLine(str);
            Console.ReadKey();
        }
    }
}

在这里插入图片描述

第20题:

using System;

namespace 练习题20
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] a = { -9, -2, 0, 1, 2, 4, 3, 9 };
            for (int i = 0; i < a.Length; i++)
            {
                if (a[i] > 0)
                    a[i] += 1;
                else if(a[i] < 0)
                    a[i] -= 1;
                else
                    a[i] = a[i];
            }
            for (int i = 0; i < a.Length; i++)
            {
                Console.WriteLine(a[i]);
            }
            Console.ReadLine();
        }
    }
}

在这里插入图片描述

第21题:

using System;

namespace 练习题21
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] names = { "我", "是", "好人" };
            string temp = "";
            for(int i=0;i<names.Length/2;i++)
            {
                temp = names[i];
                names[i] = names[names.Length - i - 1];
                names[names.Length - i - 1] = temp;
            }
            for(int i=0;i<names.Length;i++)
            {
                Console.WriteLine(names[i]);
            }
            Console.ReadKey();
        }
    }
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值