zoukankan      html  css  js  c++  java
  • C#l练习数组的比较

    用方法来实现:①有一个整数数组:{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 },找出其中最大值,并输出。不能调用数组的Max()方法码实现;

    代码实现: 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    //有一个整数数组:{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 },找出其中最大值,并输出。不能调用数组的Max()方法。
    namespace ConsoleApplication8
    {
        class Fa
        {
             public static int  max(int []b)//形参传输数组
             {
                 int c = -111;
                 for (int i = 0; i < b.Length; i++)//获得数组长度,并进行比较
                 {
                     if (b[i] > c)
                         c = b[i];
                 }
    
                 return c;
    
                
             }
         
         } 
        
        class Program
        {
            static void Main(string[] args)
            {
                int[] a = { 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 };
             
                 Console.WriteLine("最大值为{0}", Fa.max(a));
                  Console.ReadKey();//外部调用方法
            }
        }
    }
    

    实现结果:

       还可以使用另一种比较简单的方法,就是使用C#Array.Sort,升序排序

    代码实现:

     class Program
        {
            static void Main(string[] args)
            {
                int[] a = { 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 };
                Array.Sort(a);//排序
                Console.WriteLine("  最大数为 {0}", a[a.Length - 1]);//最后一个数为最大值,默认为升序
                  Console.ReadKey();
            }
        }
    

    有一个字符串数组:{ "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" },请输出最长的字符串。

    代码实现:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    //有一个整数数组:{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 },找出其中最大值,并输出。不能调用数组的Max()方法。
    namespace ConsoleApplication8
    {
        class Program
        {
            class Fa
            {
                public static void string1(string[] a1)
                {
                    int c = 0;
                    for (int i = 1; i < a1.Length; i++)//数组的个数
                    {
                        if (a1[i].Length > a1[i - 1].Length)//每个字符串的长度
                            c = i;
                    }
                    Console.WriteLine("{0}", a1[c]);//输出字符串长度最长的
                }
            }
    
            static void Main(string[] args)
            {
                string[] a = { "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" };
                Fa.string1(a);
                Console.ReadKey();
    
    
            }
        }
    }
    

    实现结果:

        

          

       

  • 相关阅读:
    前端开发拥有属于自己的云服务器能做什么?
    C语言编译、链接和运行详解
    C程序运行机制概述
    Java之顺序查找
    C语言注释
    C转义字符
    Java之二维数组基本使用
    Java之类与对象基本使用
    分享些发表技术类文章的平台
    解决蓝奏云链接无法访问问题
  • 原文地址:https://www.cnblogs.com/zykh/p/7701987.html
Copyright © 2011-2022 走看看