• 方法的调用练习


     练习一:
    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();
                    }
                }
            }
        }
    
  • 相关阅读:
    PCA手写版 Learner
    泛解析和伪静态实现二级域名(net技术)
    内存分配笔记(一)
    NVelocity的使用总结
    从discuz 里扒过来的一个通用序列化和反序列化类
    类中的执行顺序
    Nhibernate 工具 Nh profiler 使用方法
    UML中几种类间关系
    golf的23种设计模式
    net中的定时器
  • 原文地址:https://www.cnblogs.com/hao-1234-1234/p/6099466.html
Copyright © 2020-2023  润新知