C#第5周实验

来源:互联网 发布:淘宝军工 编辑:程序博客网 时间:2024/06/03 05:34

/* (程序头部注释开始)* 程序的版权和版本声明部分* Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved.* 文件名称:输入一个由若干字符组成的字符串,写一个静态方法,方法中使用数组型参数输出其中的大写字母、小写字母、数字和其他字符的个数。* 作 者: 姜甜甜* 完成日期: 2016年04 月 7 日* 版 本 号: V1.0 * 对任务及求解方法的描述部分* 程序头部的注释结束*/
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{    class Program    {        static void show(params int[] a)        {            Console.WriteLine("大写字母个数:{0},小写字母个数:{1},数字个数:{2},其他字符:{3}", a[0], a[1], a[2], a[3]);        }        static void Main(string[] args)        {            Console.WriteLine("请输入字符串:");            string s=Console.ReadLine();            int[] a={0,0,0,0};            char[] c = s.ToCharArray();            foreach (char item in c)            {                 if (item >= 'A' && item <= 'Z')                    a[0]++;                 else if( item >= 'a' && item <= 'z')                    a[1]++;                 else  if( item >= '0' && item <= '9')                    a[2]++;                 else                    a[3]++;            }            show(a);            Console.ReadKey();        }    }}

2)输入一个字符串,统计数字的个数,并输出所有的数字。

可以使用输出参数。

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{    class Program    {        public static int Getnumber(string str, out string rs)        {            int num = 0;            rs = "";            char[] a = str.ToCharArray();            for (int i = 0; i < a.Length; i++)            {                if (a[i] >= '0' && a[i] <= '9')                {                    num++;                    rs += a[i];                }            }            return num;        }        static void Main(string[] args)        {            Console.WriteLine("请输入字符串:");            string s = Console.ReadLine();            string rs;            int num = Getnumber(s, out rs);            Console.WriteLine("数字个数为:{0}个,数字有:{1}", num, rs);            Console.ReadKey();        }    }}

3)设计一个类,该类中有一个方法,该方法使用Random类随机产生10个3位数字的随机数,并把产生的10个随机数存入数组中,然后在另一个类中输出这10个数。

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        {            int[] b = new int[10];            A.getnumber(b);            foreach (int item in b)            {                Console.Write(item+" ");            }            Console.ReadKey();        }    }    public class A    {        public static void getnumber(params int[] a)        {            Random rd = new Random();            for (int i = 0; i < a.Length; i++)            {                a[i] = rd.Next(100, 1000);            }        }    }}

4)编写一个名称为MyClass的类,在该类中编写一个方法,名称为CountNum,返回值为整型,参数又两个,第一个参数可以是字符串、整型、单精度、双
精度,第二个参数为字符,方法功能返回第二个参数在第一个参数中出现次数。例如,CountChar(“6221982”,‘2’)返回值为3。

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{    class MyClass    {        public static int Countchar(string s, char st)        {            int num = 0;            char[] a = s.ToCharArray();            for (int i = 0; i < a.Length; i++)            {                if (a[i] == st)                    num++;            }            return num;        }        public static int Countchar(int inte, char st)        {            string s = inte.ToString();            int num = 0;            char[] a = s.ToCharArray();            for (int i = 0; i < a.Length; i++)            {                if (a[i] == st)                    num++;            }            return num;        }        public static int Countchar(float f, char st)        {            string s = f.ToString();            int num = 0;            char[] a = s.ToCharArray();            for (int i = 0; i < a.Length; i++)            {                if (a[i] == st)                    num++;            }            return num;        }        public static int Countchar(double d, char st)        {            string s = d.ToString();            int num = 0;            char[] a = s.ToCharArray();            for (int i = 0; i < a.Length; i++)            {                if (a[i] == st)                    num++;            }            return num;        }        static void Main(string[] args)        {            string s = Console.ReadLine();            Console.WriteLine(Countchar(s, '2'));            int s1 = int.Parse(Console.ReadLine());            Console.WriteLine(Countchar(s1, '2'));            float s2 = float.Parse(Console.ReadLine());            Console.WriteLine(Countchar(s2, '2'));            double s3 = double.Parse(Console.ReadLine());            Console.WriteLine(Countchar(s3, '2'));            Console.ReadKey();        }    }}






0 0
原创粉丝点击