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


        }
    }
}

实现结果

 

 

 

 

posted @ 2017-10-19 22:14  功不唐捐,天道酬勤  阅读(1162)  评论(0编辑  收藏  举报