.NET实验6-1

来源:互联网 发布:手游代练软件 编辑:程序博客网 时间:2024/04/30 17:22
          输入一个由若干字符组成的字符串,写一个静态方法,方法中使用输出参数输出其中的大写字母、小写字母、数字和其他字符的个数。


using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{    class Program    {        public static void Main(string[] args)        {            Console.WriteLine("请输入一串字符:");            String s = Console.ReadLine();            int bcount,scount,fig,other;            get_number(s,out bcount, out scount, out fig, out other);            Console.WriteLine("大写字母的个数为{0},小写字母的个数为{1},数字的个数为{2},其他字符的个数为{3}", bcount, scount, fig, other);            Console.ReadKey();        }        public static void get_number(string s, out int bcount, out int scount, out int fig, out int other)        {            bcount = 0;            scount = 0;            fig = 0;            other = 0;            char[] ch = new char[s.Length];            ch = s.ToCharArray();            for (int j = 0; j < s.Length; ++j)            {                if (ch[j] >= 'A' && ch[j] <= 'Z')                    ++bcount;                else if (ch[j] >= 'a' && ch[j] <= 'z')                    ++scount;                else if (ch[j] >= '0' && ch[j] <= '9')                    ++fig;                else                    ++other;            }        }    }}

运行结果:


总结:

        输出参数:在形参和实参前均加上关键字out,用out修饰符定义的参数即为输出参数。
        静态方法:静态方法不属于类的某一个具体的对象(实例),而属于类所有,所以通过类名调用静态方法,并且静态方法只能访问类中的静态成员。

0 0
原创粉丝点击