C#实验四

 

实验4-1

假设有一个GetNumber方法(参数为字符串strSource),该方法可以用来统计字符串strSource中数字字符的个数。请自行设计一个控制台应用验证图1方法的正确性。

/*  
 *Copyright(c)2018,烟台大学计算机学院  
 *All right reserved.  
 *文件名称:ConsoleApp1.cpp  
 *作者:李小同  
 *完成日期;2018年9月18日  
 *版本号;v1.1  
 *  
 *问题描述:如下  
 *输入描述:功能需求  
 *程序输出:所需功能的实现  
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = Console.ReadLine();
            string ss;
            Console.WriteLine("该字符串中的数字字符的个数为{0}: ", GetNumber(str, out ss));
            Console.WriteLine(ss);
            Console.ReadKey();
        }
        public static int GetNumber(string strSource, out string n1)
        {
            try
            {
                int n = 0;
                n1 = "";
                char[] chars = strSource.ToCharArray();
                foreach (char item in chars)
                {
                    if (item >= ’0’ && item <= ’9’)
                        n++;
                }
                return n;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}

 

实验4-2

产生若干个3位数字,并在控制台中输出,请完成折叠处代码。首先,如图1所示,设计一个Class2类,该类中有1个名称为Method01方法。该方法使用Random类随机产生n个3位数字(如636)的随机数,并把产生的n个随机数存入数组中。然后,编写一个控制台应用,调用Class2类中Method01方法,产生10个随机数,然后编写代码输出数组a中的各个元素。

/*  
 *Copyright(c)2018,烟台大学计算机学院  
 *All right reserved.  
 *文件名称:ConsoleApp1.cpp  
 *作者:李小同  
 *完成日期;2018年9月18日  
 *版本号;v1.1  
 *  
 *问题描述:如下  
 *输入描述:功能需求  
 *程序输出:所需功能的实现  
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] num = new int[11];
            A.show(num);
            Console.ReadKey();
        }
    }
    class A
    {
        public static void GetRom(int[] num)
        {
            Random rd = new Random();
            for (int i = 0; i < 10; i++)
            {
                num[i] = rd.Next(100, 999);
            }
        }

        public static void show(int[] num)
        {
            GetRom(num);
            for (int i = 0; i < 10; i++)
            {
                Console.Write(num[i]);
                Console.Write(" ");
            }
        }

    }

}

 

实验4-3

编写一个名称为Class3的类,在该类中编写一个方法,名称为getCountChar,返回值为整型,参数有两个,第一个参数可以是字符串,第二个参数为字符,方法返回值为第二个参数在第一个参数中出现次数。例如,CountChar("6221982",'2')返回值为3。

/*  
 *Copyright(c)2018,烟台大学计算机学院  
 *All right reserved.  
 *文件名称:ConsoleApp1.cpp  
 *作者:李小同  
 *完成日期;2018年9月18日  
 *版本号;v1.1  
 *  
 *问题描述:如下  
 *输入描述:功能需求  
 *程序输出:所需功能的实现  
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Myclass mc = new Myclass();
            Console.WriteLine("请输入");
            string str1 = Console.ReadLine();
            Console.WriteLine("请输入您要查找的字符");
            char ch1 = (char)Console.Read();
            mc.getCountChar(str1, ch1);
        }
    }
    class Myclass
    {
        public int getCountChar(string str, char ch)
        {
            try
            {
                int n = 0;
                char[] cha = str.ToCharArray();
                foreach (char cha1 in cha)
                {
                    if (cha1 == ch)
                        ++n;
                }
                Console.WriteLine("您要查找的字符有{0}个", n);
                return n;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}

实验4-4

输入一个字符串。 编写一个静态方法,统计其中字母个数、数字个数。要求:使用输出参数输出其中的字母个数、数字个数。

 

/*  
 *Copyright(c)2018,烟台大学计算机学院  
 *All right reserved.  
 *文件名称:ConsoleApp1.cpp  
 *作者:李小同  
 *完成日期;2018年9月18日  
 *版本号;v1.1  
 *  
 *问题描述:如下  
 *输入描述:功能需求  
 *程序输出:所需功能的实现  
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                int n1;
                int n2;
                Console.Write("请输入一串数字: ");
                string s1 = Console.ReadLine();
                char[] a = s1.ToCharArray();
                n1 = 0;
                n2 = 0;
                for (int i = 0; i < s1.Length; i++)
                {
                    if (((char)s1[i] > 'a' && (char)s1[i] < 'z') || ((char)s1[i] > 'A' && (char)s1[i] < 'Z'))
                    {
                    n1++;
                }
                if ((int)s1[i] > '0' && (int)s1[i] < '9')
                    {
                    n2++;
                }
                    else
                    {

                }
            }
                Console.WriteLine("zimu={0} ,shuzi={1}", n1, n2);
            Console.ReadKey();
        }
            catch (Exception ex)
            {
                throw ex;
            }
}
    }
}

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值