方法的调用练习

 练习一:
class Program { //虽然是公有字段,但方法中失效。 public static int _number = 10; static void Main(string[] args) { int a = 3; int res = Test(a); Console.WriteLine(res); // Console.WriteLine(_number); //输出:10 Console.ReadKey(); } public static int Test(int a) { a = a + 5; return a; // Console.WriteLine(_number); //无输出 } public static void TestTwo() { // Console.WriteLine(_number); //无输出 } }

 

练习二:

    class Program
        {
            static void Main(string[] args)
            {
                //举例:写一个方法,判断一个年份是否是润年.

                bool b = IsRun(2001);
                Console.WriteLine(b);
                Console.ReadKey();

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


        }

 练习三

class Program
    {
        public static void Main(string[] args)
        {
            //比较两个数字的大小 返回最大的
            //int a1 = 10;
            //int a2 = 20;
            int max = GetMax(10, 20);//实参
            Console.WriteLine(max);
            Console.ReadKey();
        }


        /// <summary>
        /// 计算两个整数之间的最大值 并且返回最大值
        /// </summary>
        /// <param name="n1">第一个数</param>
        /// <param name="n2">第二个数</param>
        /// <returns>返回的最大值</returns>
        public static int GetMax(int n1, int n2)//形参
        {
            int max = n1 > n2 ? n1 : n2;//如果 n1 > n2成立,则输出n1,否则输出n2

            return max;
        }
    }

 练习四

 class Program
    {
        static void Main(string[] args)
        {
            //1 读取输入的整数
            //多次调用(如果用户输入的是数字,则返回,否则提示用户重新输入)

            Console.WriteLine("请输入一个数字");
            string input = Console.ReadLine();
            int number = GetNumber(input);
            Console.WriteLine(number);
            Console.ReadKey();
        }

        /// <summary>
        /// 这个方法需要判断用户的输入是否是数字
        /// 如果是数字,则返回
        /// 如果不是数字,提示用户重新输入
        /// </summary>
        public static int GetNumber(string s)
        {
            while (true)
            {
                try
                {
                    int number = Convert.ToInt32(s);
                    return number;
                }
                catch
                {
                    Console.WriteLine("请重新输入");
                    s = Console.ReadLine();
                }
            }
        }


    }

 练习五

 class Program
    {
        static void Main(string[] args)
        {
        
            //只能让用户输入yes或者no,只要不是,就重新输入


            //Console.WriteLine("请输入yes或者no");
            //string str = Console.ReadLine();
            //string result = IsYerOrNo(str);
            //Console.WriteLine(result);
            //Console.ReadKey();

            //4计算输入数组的和:int Sum(int[] values)
            int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            int sum = GetSum(nums);
            Console.WriteLine(sum);
            Console.ReadKey();
        }

        /// <summary>
        /// 计算一个整数类型数组的总和
        /// </summary>
        /// <param name="nums">要求总和的数组</param>
        /// <returns>返回这个数组的总和</returns>
        public static int GetSum(int[] nums)
        {
            int sum = 0;
            for (int i = 0; i < nums.Length; i++)
            {
                sum += nums[i];
            }
            return sum;
        }



        /// <summary>
        /// 限定用户只能输入yes或者no 并且返回
        /// </summary>
        /// <param name="input">用户的输入</param>
        /// <returns>返回yes或者no</returns>
        public static string IsYerOrNo(string input)
        {
            while (true)
            {
                if (input == "yes" || input == "no")
                {
                    return input;
                }
                else
                {
                    Console.WriteLine("只能输入yes或者no,请重新输入");
                    input = Console.ReadLine();
                }
            }
        }
    }

 

转载于:https://www.cnblogs.com/hao-1234-1234/p/6099466.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值