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

    实现结果

     

     

  • 相关阅读:
    vijos 1426
    2455 繁忙的都市
    2104 删除物品
    3235 战争
    BZOJ 2962
    COGS 265 线段覆盖
    P2184 贪婪大陆
    0729模拟赛解题报告
    BZOJ 1012
    BZOJ 2763
  • 原文地址:https://www.cnblogs.com/zykh/p/7695803.html
Copyright © 2011-2022 走看看