zoukankan      html  css  js  c++  java
  • C#练习记录(统计字符串中的字符数和计算最大值)

    请用户输入一个字符串,计算字符串中的字符个数,并输出

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication7
    {
        
        class Program
        {
            static void Main(string[] args)
            {
                string strs;
                Console.WriteLine("请输入一个字符串");
                strs = Console.ReadLine();
                Console.WriteLine("刚才输入的字符串的内容是{0},字符个数为{1}", strs,strs.Length);
                Console.ReadKey();
    
    
    
            }
        }
    }
    

     

    用方法来实现:计算两个数的最大值。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication7
    {
        class Fa
        {
            public static int Max(int a, int b)
            {
                if (a > b)
                    return a;
                else
                    return b;
            }
    
        }
        class Program
        {
            static void Main(string[] args)
            {
                int a;
                int b;
                Console.WriteLine("请分别输入两个数的值");
                a = Convert.ToInt32(Console.ReadLine());
                b = Convert.ToInt32(Console.ReadLine());
                Console.Write("输入的两个数的值分别为a={0}, b={1}", a, b);
                Console.WriteLine("它们的最大值为max={0}",Fa.Max(a, b));
                Console.ReadKey();
    
    
            }
        }
    }

      计算多个数的最大值

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication7
    {
        class Fa
        {
            public static int Max(params int[] a1)
            {  int max=-111;
            for (int i=0;i<a1.Length;i++)
             if(a1[i]>max)
                 max=a1[i];
            return max;
    
           
            }
    
        }
        class Program
        {
            static void Main(string[] args)
            {
                  int[] a = { 23, 45, 36, 34, 35 };
                Console.WriteLine("它们分别为");
                for (int i = 0; i < a.Length; i++)
                {
                    Console.Write("{0} ", a[i]);
                }
                Console.WriteLine("它们的最大值为max={0}",Fa.Max(23,45,36,34,35));
               Console.WriteLine("他们的最大值为max={0}",Fa.Max(a));
                  
                Console.ReadKey();
    
    
            }
        }
    }
    

    实现结果

     

     

  • 相关阅读:
    Gitlab安装与备份恢复
    Logstash使用grok插件解析Nginx日志
    SSH登录启用Google二次身份验证
    Nginx隐藏标识以及其版本号
    Nginx虚拟目录设置
    Tomcat虚拟目录设置
    sqlserver无法连接
    Howto: 如何将ArcGIS Server缓存移动到新服务器
    优酷网架构学习笔记
    .net中自定义过滤器对Response内容进行处理
  • 原文地址:https://www.cnblogs.com/zykh/p/7695803.html
Copyright © 2011-2022 走看看