2020-11-12

方法的调用

1、我们在main()函数中调用test()函数,我们称main()为调用者,管test()叫被调用者。
例:(错误代码)

class Program
    {
        static void Main(string[] args)//所有的方法都要在main函数里面调用
        {
            int a = 3;//a的作用域就在main函数里面,其他访问不到
            Test();
            Console.WriteLine("方法的test");
        }
        public static void Test() {
            a = a + 5;
        }
       
    }

注:如果被调用者test()想要得到被调用者的值,有以下两种方法:
(1)、传参

class Program
    {
        static void Main(string[] args)//所有的方法都要在main函数里面调用,并且要进行传参
        {
            int a = 3;//a的作用域就在main函数里面,其他访问不到
            Test(a);
            Console.WriteLine(a);
        }
        public static void Test(int a) {
            a = a + 5;
        }
       
    }

(2)、使用静态字段来模拟全局变量(c#中没有全局变量)
注:如果调用者想要得到被调用的值,直接使用返回值即可。

2、不管是形参还是实参都是在内存中开辟空间的
例:

class Program
    {
      
        static void Main(string[] args)//所有的方法都要在main函数里面调用(类名.方法名)
        {
            int a1 = 10;//实参
            int a2=60;
            int a=Program.GetMax(a1, a2);          
            Console.WriteLine(a);
        }
        public static int GetMax(int n1,int n2)//形参
        {
            int max = n1 > n2 ? n1 : n2;//三元表达式
            return max;//让main()得到max的值
        }
     
    }

3、方法的功能一定要单一。方法中最忌讳“提示用户输入”。不能用于非控制台应用。因此可以将提示输入的语句放进main()中。
例1:

class Program
    {      
    static void Main(string[] args)//所有的方法都要在main函数里面调用(类名.方法名)
        {
            //读取输入的整数
            //多次调用(如果用户输入的是数字,则返回,否则提示用户重新输入)
            Console.WriteLine("请您输入一个数:");
            string input = Console.ReadLine();
            int num=NewMethod(input);
            Console.WriteLine(num);
           
        }      
        public static int NewMethod(string s)//形参
        {
            while (true) {
                try
                {
                    int num = Convert.ToInt32(s);//读取输入的整数。
                    return num;
                }
                catch (Exception)
                {
                    Console.WriteLine("输入有误!重来!");
                    s = Console.ReadLine();//重新输入一个新的s
                }

            }
        }

    }

例2:

class Program
    {            static void Main(string[] args)//所有的方法都要在main函数里面调用(类名.方法名)
        { Console.WriteLine("输入yesorno");
            string str = Console.ReadLine();
            GetYesorNo(str);
          
        }
 //只允许用户输入yes或者no,只要不是就重新输入
        public static string GetYesorNo(string input)
        {
            while (true)
            {
                if (input == "yes" || input == "no")
                {
                    return input;
                }
                else {
                    Console.WriteLine("只允许用户输入yes或者no,重新输入");
                    input = Console.ReadLine();
                }
            }
            
        }
        
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值