美文网首页
2018-12-13(练习1 方法)

2018-12-13(练习1 方法)

作者: youthisY | 来源:发表于2018-12-27 17:53 被阅读0次

题目

做一个 方法

代码

namespace _2018_12_13_练习1
{
    class Program
    {
        static void Main(string[] args)
        {/*
            *在保存了多个学生姓名的数组中,指定查找区间,查找某个学生姓名并显示是否查找成功
            * */



            //1 定义一个数组
            string[] list = { "张三", "猪八戒", "妲己", "海王" };
            bool resule = Search(list ,1,2,"海王");

            Console.Write(resule);
            Console.ReadKey();
        }
        /*
         * 在指定去检查找某个学生姓名,如果找到,返回true,如果没有找到,返回 false
         * list:保存学生姓名的数组
         * start:查找区间开始的位置
         * len:查找区间的长度
         * username:需要查找的姓名
         * */
        static bool Search(string[] list, int start, int len, string username)
        {
            bool ret = false;
            if ((start + len) > list.Length)
            {
                ret = false;
            }
            else
            {
                for (int i = start; i < start + len; i++)
                {
                    if (username == list[i])
                    {
                        ret = true;
                        break;
                    }
                }

            }
            return ret;
        }
    }
}

效果图

相关文章

网友评论

      本文标题:2018-12-13(练习1 方法)

      本文链接:https://www.haomeiwen.com/subject/aihdlqtx.html