C#学习笔记六(2018.6.21)

C Sharp

Anything one man can imagine, other men can make real.

冒泡排序

int[] nums={9,8,7,6,5,4,3,2,1,0};---> 0,1,2,3,4,5,6,7,8,9
第一趟比较:8 7 6 5 4 3 2 1 0 9 交换了9次——i=0 j=nums.Length-1-0;
第二趟比较:7 6 5 4 3 2 1 0 8 9 交换了9次——比较了 但是没交换 i=1 j=8 j=nums.Length-1-1;
第三趟比较:6 5 4 3 2 1 0 7 8 9 交换了7次——i=2 j=7 j=nums.Length-1-2
第四趟比较:5 4 3 2 1 0 6 7 8 9 交换了6次——i=3 j=6
第五趟比较:4 3 2 1 0 5 6 7 8 9 交换了5次——i=4 j=5
第六趟比较:3 2 1 0 4 5 6 7 8 9 交换了4次
第七趟比较:2 1 0 3 4 5 6 7 8 9 交换了3次
第八趟比较:1 0 2 3 4 5 6 7 8 9 交换了2次
第九趟比较:0 1 2 3 4 5 6 7 8 9 交换了1次

for(int i=0;i<nums.Length-1;i++)
{
    for(int j=0;j<nums.Length-1-i;j++)
    {
        if(nums[j]>nums[j+1])
        {
            int temp=nums[j];
            nums[j]=nums[j+1];
            nums[j+1]=temp;
        }
    }
}

方法(函数)

[public] static 返回值类型  方法名([参数列表])
{
    方法体;
}

public:访问修饰符,公开的,公共的
static:表示静态
返回值类型:如果没有返回值,写void
方法名:Pascal,要求每个单词的首字母都要大写。
参数列表:完成这个方法,所必须要提供给这个方法的条件。哪怕方法中不需要参数,小括号也不能省略。
方法的调用类名.方法名([参数列表]);
return
1)、在方法中返回要返回的值
2)、立即结束当前方法

调用者和被调用者的关系

我们在Main()函数中调用Test()函数,我们管Main()函数叫做调用者,管Test()函数叫做调用者,管Test被调用者。
如果被调用者想要得到调用者中的值:
1、传递参数
2、声明一个静态的字段,当做”全局变量”使用。
如果调用者想要得到被调用者中的值:
1、写返回值。
注:
形参:形式上的参数,也会在内存中开辟空间。
实参:调用函数的时候传入的参数。

注:写方法的时候需要注意
1、方法的功能一定要单一。
2、在方法中尽量的避免出现提示用户输入之类的代码。

out参数

可以帮助我们在一个方法中返回多个值,不限类型。
使用out参数的时候要求,out参数必须在方法内为其赋值。

今日代码

namespace ABC_Low_Level4
{
    class Program
    {
        static void Main(string[] args)
        {
            /*2018.06.21--->数组,函数(方法)
             * 传智播客基础班第六天
             */

            //从一个整数数组中取出最大的整数,最小整数,总和,平均值
            int[] nums = { 2, 4, 9, 15, 3, 8, 0, -1 };
            int max = nums[0],
                sum = 0,
                min = nums[0];
            double count = 0;
            for (int i = 0; i < nums.Length; i++)
            {
                if (nums[i] > max)
                {
                    max = nums[i];
                }
                if (nums[i] < min)
                {
                    min = nums[i];
                }
                sum += nums[i];
                count = sum / nums.Length;
            }
            Console.WriteLine("最大整数为{0},最小整数为{1},总和为{2},平均值为{3}", max, min, sum, count);
            Console.ReadKey();



            //写一个方法,判断给你的年份是否是闰年
            Console.WriteLine("请输入一个年份");
            int year = Convert.ToInt32(Console.ReadLine());
            bool b = IsRun(year);
            Console.WriteLine(b);
            Console.ReadKey();


            //读取输入的整数,定义成方法,多次调用(如果用户输入的是数字,则返回,否则提示用户重新输入)
            Console.WriteLine("请输入一个数字");
            string str = Console.ReadLine();
            int n = GetNumber(str);
            Console.WriteLine(n);
            Console.ReadKey();


            //只允许用户输入y或者n,改成方法
            Console.WriteLine("请输入yes或者是no");
            string input = Console.ReadLine();
            string strnew = IsYorN(input);
            Console.WriteLine("您输入的是{0}",strnew);
            Console.ReadKey();


            //学习out参数
            int n1 = 0;
            string s = Test(out n1, out b);
            Console.WriteLine(s);
            Console.WriteLine(n);
            Console.WriteLine(b);
            Console.ReadKey();

            //判断登陆是否成功
            Console.WriteLine("请输入用户名");
            string username = Console.ReadLine();
            Console.WriteLine("密码");
            string userpwd = Console.ReadLine();
            string msg;
            bool basd = IsLogin(username, userpwd, out msg);
            Console.WriteLine("登陆结果:{0}", basd);
            Console.WriteLine("登陆信息:{0}", msg);
        }

        /// <summary>
        /// 判断给定的年份是否是闰年
        /// </summary>
        /// <param name="year">要判断的年份</param>
        /// <returns>是否是闰年</returns>
        public static bool IsRun(int year)
        {
            if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 将用户输入的字符串转换成int类型
        /// </summary>
        /// <param name="strNumber">用户输入的字符串</param>
        /// <returns>返回转换成功后的整数</returns>
        public static int GetNumber(string strNumber)
        {
            while (true)
            {
                try
                {
                    int number = Convert.ToInt32(strNumber);
                    return number;
                }
                catch
                {
                    Console.WriteLine("输入有误,请重新输入");
                    strNumber = Console.ReadLine();
                }
            } 
        }   

        /// <summary>
        /// 判断用户输入的是否是yes或者no
        /// </summary>
        /// <param name="input">要判断的字符串</param>
        /// <returns>yes或者no</returns>
        public static string IsYorN(string input)
        {
            while (true)
            {
                if(input != "yes" && input != "no")
                {
                    Console.WriteLine("输入有误,只能输入yes或者no");
                    input = Console.ReadLine();
                }
                else
                {
                    return input;
                }
            }
        }
        /// <summary>
        /// 学习out参数
        /// </summary>
        /// <param name="number"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static string Test(out int number, out bool b)
        {
            number = 100;
            b = false;
            return "张三";
        }

        /// <summary>
        /// 判断登陆是否成功
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="userPwd">密码</param>
        /// <param name="msg">多余要返回的登陆信息</param>
        /// <returns>登陆是否成功</returns>
        public static bool IsLogin(string userName,string userPwd,out string msg)
        {
            if (userName == "admin" && userPwd == "admin")
            {
                msg = "登陆成功";
                return true;
            }
            else if(userName == "admin")
            {
                msg = "登陆失败,密码错误";
                return false;
            }
            else if(userPwd =="admin")
            {
                msg = "登陆失败,用户名错误";
                return false;
            }
            else
            {
                msg = "未知错误";
                return false;
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/cch860846552/article/details/80764042
今日推荐