zoukankan      html  css  js  c++  java
  • c#基础加强版之方法控制流复习-1关于方法的讲解

     题一:我的code

    /// <summary>
      /// 使用方法输入一个整数,使用方法输入一个范围内的数,比如(16-25)之内的数字。
      /// </summary>
        class Program
        {
            static int ReadInt(){
                do {
                    Console.WriteLine("请输入一个整数");
                    try {
                      int  inputNum = Convert.ToInt32(Console.ReadLine());
                       return inputNum;
                    }
                    catch  {
                        Console.WriteLine("您的输入有误");
                    }   
                } while (true);
            }
    
            static int ReadInt(int lowLimit,int highLimit){
                do {
                    Console.WriteLine("请输入16-65的数字");
                    try  {
                        int inputNum = Convert.ToInt16(Console.ReadLine());
                        if (inputNum>lowLimit && inputNum<highLimit){
                            return inputNum;
                        }
                        else {
                            Console.WriteLine("您输入的数字不在范围之内");
                        }
                    }
                    catch (System.Exception ex){
                        Console.WriteLine(ex.Message);
                    }
                } while (true);
            }
            static void Main(string[] args)
            {
                int number = ReadInt();
                Console.WriteLine("您刚刚输入的" + number);
                int number1 = ReadInt(16, 150);
                Console.WriteLine("您刚刚输入的数字" + number1);
                Console.ReadKey();
            }
        }
    }
    View Code

    题二:我的code

    namespace 练习题二
    {
        /// <summary>
        /// 输入一个年份,一个月份,计算该月的天数
        /// </summary>
        class Program
        {
            static int ReadInt(string str) {        //读取年份与月份;
           do{ 
               Console.WriteLine("请输入"+str);
              try {
                int inputNum = Convert.ToInt32(Console.ReadLine());
                return inputNum;
                 }
                 catch (System.Exception ex){
                 Console.WriteLine(ex.Message);
                  }
           } while (true);
            }
    
            static int GetDays(string year, int month) { //计算天数
                int days = 0;
                switch (month) {
                    case 1:
                    case 3:
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                    case 12: 
                     days=31;break;
                    case 4:
                    case 6:
                    case 9:
                    case 11:
                    days=30;break;
                    case 2:
                        if (year=="pingNian"){
                            days=28;
                        }
                        days=29;break;
                    default: Console.WriteLine("输入有误"); break;
                }
                return days;
            }
    
            static string TestYear(int year) {//判断年份是平年还是闰年
              if (year%4==0 && year%100!=0 || year%400==0){
                return "yunNian";
             } 
             else {
                return "pingNian";
                }
            }
    
            static void Main(string[] args)
            {
                string yearP="年份";
                string monthP="月份";
                int year = ReadInt( yearP );
                int month = ReadInt(monthP);
                string YearType = TestYear(year);
                int days = GetDays(YearType, month);
                Console.WriteLine("该月有" + days);
                Console.ReadKey();
            }
        }
    }
    View Code

    题一:老师的讲解如下
    1.首先 编写最基本能实现功能的代码块

    static void Main(string[] args)
            {
                int number = 0;//将number变量声明while循环外,才能在整个方法域内使用
                Console.WriteLine("请输入一个数字");
                string str = Console.ReadLine();
                while (true) { 
                    try {
                        number = Convert.ToInt32(str);
                        //如果变量在方法中,一定要注意其只作用在包含它的最小括号;
                        //int number = Convert.ToInt32(str);   // 所以,此处必须将number变量定义在while循环外. 
                        // 否则在while循环外打印number变量是不可行的。
                        break;
                    }
                    catch (System.Exception ex) {
                        Console.WriteLine("您的输入有误,请重新输入");
                        str = Console.ReadLine();
                    }
                  }
                Console.WriteLine("您输入的数字是" + number);
              }

    2.然后思考,将以上代码封装在一个方法内部.方法是重用的机制,通俗的说用一个方法名来代替一段代码(书本的对方法的定义为:具有名称的代码块,此处就很好地体现了这个概念的意思)

    ->语法复习:

        ->由static修饰的成员只允许访问static成员,因为main函数为静态的,即为static的,我们要写的方法是可以被main能访问到的,所以也必须声明为静态的.

        ->方法一般声明为public,以便方法没有访问限制,随处可以调用.此处练习,方法是写在同一类中,我们就不必加访问修饰符以声明为public公有成员.这样默认为               private私有成员,privata私有成员允许在同一个类中的任何其他成员访问.

       将以上读取数字的代码封装成一个方法后的代码:

    namespace 老师的代码
    {
        class Program
        {
            static void MyFirstMehod() {
                int number = 0;//将number变量声明while循环外,才能在整个方法域内使用
                Console.WriteLine("请输入一个数字");
                string str = Console.ReadLine();
                while (true)
                {
                    try
                    {
                        number = Convert.ToInt32(str);
                        //如果变量在方法中,一定要注意其只作用在包含它的最小括号;
                        //int number = Convert.ToInt32(str);   // 所以,此处必须将number变量定义在while循环外. 
                        // 否则在while循环外打印number变量是不可行的。
                        break;
                    }
                    catch (System.Exception ex)
                    {
                        Console.WriteLine("您的输入有误,请重新输入");
                        str = Console.ReadLine();
                    }
                }
                Console.WriteLine("您输入的数字是" + number);
    
            }
            static void Main(string[] args)
            { 
                #region 过程方式
                //int number = 0;//将number变量声明while循环外,才能在整个方法域内使用
                //Console.WriteLine("请输入一个数字");
                //string str = Console.ReadLine();
                //while (true) { 
                //    try {
                //        number = Convert.ToInt32(str);
                //        //如果变量在方法中,一定要注意其只作用在包含它的最小括号;
                //        //int number = Convert.ToInt32(str);   // 所以,此处必须将number变量定义在while循环外. 
                //        // 否则在while循环外打印number变量是不可行的。
                //        break;
                //    }
                //    catch (System.Exception ex) {
                //        Console.WriteLine("您的输入有误,请重新输入");
                //        str = Console.ReadLine();
                //    }
                //  }
                //Console.WriteLine("您输入的数字是" + number)
            #endregion
                MyFirstMehod();//过程方式里的代码被MyFirstMethod所替代了.
              }
        }
    }
    View Code

     3.继续引申思考:给定一个最大数,输入非负数

    ->方法的定义:

        ->功能:完成一个数字的输入,并返回,如果错误提示,并重新输入.因此,此时的方法有返回值,声明的方法时需要声明返回值类型,而不再是void.

         代码完成如下:

    namespace 老师的代码
    {
        class Program
        {
            static void MyFirstMehod() {
                int number = 0;//将number变量声明while循环外,才能在整个方法域内使用
                Console.WriteLine("请输入一个数字");
                string str = Console.ReadLine();
                while (true)
                {
                    try
                    {
                        number = Convert.ToInt32(str);
                        //如果变量在方法中,一定要注意其只作用在包含它的最小括号;
                        //int number = Convert.ToInt32(str);   // 所以,此处必须将number变量定义在while循环外. 
                        // 否则在while循环外打印number变量是不可行的。
                        break;
                    }
                    catch (System.Exception ex)
                    {
                        Console.WriteLine("您的输入有误,请重新输入");
                        str = Console.ReadLine();
                    }
                }
                Console.WriteLine("您输入的数字是" + number); 
            }
    
            static int MyFirstMehod(int max)      //方法的重载,给定一个最大数,输入非负数
            {
                int number = 0;
                Console.WriteLine("请输入0-{0}之间的一个数字", max);
                string str = Console.ReadLine();
                while (true)  {
                    try
                    {
                        number = Convert.ToInt32(str);
                        if (number > 0 && number < max){
                            return number;
                        }
                        else {
                            Console.WriteLine("您输入的数字不在范围之内,请重新输入");
                            str = Console.ReadLine();
                        }
                    }
                    catch
                    {
                        Console.WriteLine("您的输入有误,请重新输入");
                        str = Console.ReadLine();
                    }
                }
    
            }
            static void Main(string[] args)
            { 
                #region 过程方式
                //int number = 0;//将number变量声明while循环外,才能在整个方法域内使用
                //Console.WriteLine("请输入一个数字");
                //string str = Console.ReadLine();
                //while (true) { 
                //    try {
                //        number = Convert.ToInt32(str);
                //        //如果变量在方法中,一定要注意其只作用在包含它的最小括号;
                //        //int number = Convert.ToInt32(str);   // 所以,此处必须将number变量定义在while循环外. 
                //        // 否则在while循环外打印number变量是不可行的。
                //        break;
                //    }
                //    catch (System.Exception ex) {
                //        Console.WriteLine("您的输入有误,请重新输入");
                //        str = Console.ReadLine();
                //    }
                //  }
                //Console.WriteLine("您输入的数字是" + number)
            #endregion
                MyFirstMehod();//过程方式里的代码被MyFirstMethod所替代了.
                int returnNumber = MyFirstMehod(100);
                Console.ReadKey();
              }
        }
    }
    View Code

     以上代码中的static void MyFirstMethod()与static int MyFirstMethod(int max)是方法的重载,方法重载的定义:在同一个类中声明两个或多个方法名相同的方法,要求这些同名方法保证有一个签名与其他的方法不同.签名是是指方法的名称,参数的个数,参数的类型与顺序.参数的修饰符.注意签名不包括方法的返回值类型.通俗的解释:方法的重载实际上是一些不同的方法,只是由于功能相似,将名字写成一样,便于开发者调用.

     

    4.给定一个范围,输入范围之内的数字

    代码如下:

    namespace 老师的代码
    {
        class Program
        {
            static void MyFirstMehod() {
                int number = 0;//将number变量声明while循环外,才能在整个方法域内使用
                Console.WriteLine("请输入一个数字");
                string str = Console.ReadLine();
                while (true)
                {
                    try
                    {
                        number = Convert.ToInt32(str);
                        //如果变量在方法中,一定要注意其只作用在包含它的最小括号;
                        //int number = Convert.ToInt32(str);   // 所以,此处必须将number变量定义在while循环外. 
                        // 否则在while循环外打印number变量是不可行的。
                        break;
                    }
                    catch (System.Exception ex)
                    {
                        Console.WriteLine("您的输入有误,请重新输入");
                        str = Console.ReadLine();
                    }
                }
                Console.WriteLine("您输入的数字是" + number); 
            }
    
            static int MyFirstMehod(int max)      //方法的重载,给定一个最大数,输入非负数
            {
                int number=0 ;
                Console.WriteLine("请输入0-{0}之间的一个数字", max);
                string str = Console.ReadLine();
                while (true)  {
                    try {
                        number = Convert.ToInt32(str);
                        if (number > 0 && number < max){
                            break;
                        }
                        else {
                            Console.WriteLine("您输入的数字不在范围之内,请重新输入");
                            str = Console.ReadLine();
                        }
                    }
                    catch{
                        Console.WriteLine("您的输入有误,请重新输入");
                        str = Console.ReadLine();
                    }
                }
                return number;
            }
    
            static int MyFirstMehod(int min, int max) {//方法重载,输入一个范围内的数字
                 int number;
                Console.WriteLine("请输入一个15-65之间的数字");
                string str = Console.ReadLine();
                while (true)
                {     
                    try
                    {
                        number=Convert.ToInt32(str);
                        if (number>min && number<max)
                        {
                            break;
                        }
                        else {
                            Console .WriteLine("您的输入的数字不在范围之内,请重新输入");
                            str = Console.ReadLine(); 
                        }
                        
                    }
                    catch 
                    {
                        Console.WriteLine("您的输入有误,请重新输入");
                        str = Console.ReadLine(); 
                    }
                
                }
    
                return number;
            }
            static void Main(string[] args)
            { 
                #region 过程方式
                //int number = 0;//将number变量声明while循环外,才能在整个方法域内使用
                //Console.WriteLine("请输入一个数字");
                //string str = Console.ReadLine();
                //while (true) { 
                //    try {
                //        number = Convert.ToInt32(str);
                //        //如果变量在方法中,一定要注意其只作用在包含它的最小括号;
                //        //int number = Convert.ToInt32(str);   // 所以,此处必须将number变量定义在while循环外. 
                //        // 否则在while循环外打印number变量是不可行的。
                //        break;
                //    }
                //    catch (System.Exception ex) {
                //        Console.WriteLine("您的输入有误,请重新输入");
                //        str = Console.ReadLine();
                //    }
                //  }
                //Console.WriteLine("您输入的数字是" + number)
            #endregion
                MyFirstMehod();//过程方式里的代码被MyFirstMethod所替代了.
    
                int returnNumber1= MyFirstMehod(100);
                Console.WriteLine("您刚刚输入的是" + returnNumber1);
     
                int returnNumber2= MyFirstMehod(15, 65);
                Console.WriteLine("您刚刚输入的是" + returnNumber2);
                Console.ReadKey();
              }
        }
    }
    View Code

     思考:代码中MyFirstMethod()与MyFirstMthod(int max)与MyFirstMethod(int max,int min)三个重载方法代码基本相似.因此,我可以尝试用通用的语法来实现代码的调用.我们可以单独用MyFirstMethod(int max,int min)来实现其他两个重载方法的功能.

    代码如下:

    namespace 老师的代码
    {
        class Program
        {
            static int MyFirstMehod() {
    #region   旧方法
                //int number = 0;//将number变量声明while循环外,才能在整个方法域内使用
                //Console.WriteLine("请输入一个数字");
                //string str = Console.ReadLine();
                //while (true)
                //{
                //    try
                //    {
                //        number = Convert.ToInt32(str);
                //        //如果变量在方法中,一定要注意其只作用在包含它的最小括号;
                //        //int number = Convert.ToInt32(str);   // 所以,此处必须将number变量定义在while循环外. 
                //        // 否则在while循环外打印number变量是不可行的。
                //        break;
                //    }
                //    catch (System.Exception ex)
                //    {
                //        Console.WriteLine("您的输入有误,请重新输入");
                //        str = Console.ReadLine();
                //    }
                //}
                //Return number;
    #endregion
                return MyFirstMehod(int.MinValue, int.MaxValue);
            }
    
            static int MyFirstMehod(int max)      //方法的重载,给定一个最大数,输入非负数
            {
    #region 旧方法
                //int number = 0;
                //Console.WriteLine("请输入0-{0}之间的一个数字", max);
                //string str = Console.ReadLine();
                //while (true)
                //{
                //    try
                //    {
                //        number = Convert.ToInt32(str);
                //        if (number > 0 && number < max)
                //        {
                //            break;
                //        }
                //        else
                //        {
                //            Console.WriteLine("您输入的数字不在范围之内,请重新输入");
                //            str = Console.ReadLine();
                //        }
                //    }
                //    catch
                //    {
                //        Console.WriteLine("您的输入有误,请重新输入");
                //        str = Console.ReadLine();
                //    }
                //}
                //return number;
    #endregion
                return MyFirstMehod(0, max);
            }
    
            static int MyFirstMehod(int min, int max) {//方法重载,输入一个范围内的数字
                 int number;
                Console.WriteLine("请输入一个{0}-{1}之间的数字",min,max);
                string str = Console.ReadLine();
                while (true)
                {     
                    try
                    {
                        number=Convert.ToInt32(str);
                        if (number>min && number<max)
                        {
                            break;
                        }
                        else {
                            Console .WriteLine("您的输入的数字不在范围之内,请重新输入");
                            str = Console.ReadLine(); 
                        }
                        
                    }
                    catch 
                    {
                        Console.WriteLine("您的输入有误,请重新输入");
                        str = Console.ReadLine(); 
                    }
                
                }
    
                return number;
            }
            static void Main(string[] args)
            { 
                #region 过程方式
                //int number = 0;//将number变量声明while循环外,才能在整个方法域内使用
                //Console.WriteLine("请输入一个数字");
                //string str = Console.ReadLine();
                //while (true) { 
                //    try {
                //        number = Convert.ToInt32(str);
                //        //如果变量在方法中,一定要注意其只作用在包含它的最小括号;
                //        //int number = Convert.ToInt32(str);   // 所以,此处必须将number变量定义在while循环外. 
                //        // 否则在while循环外打印number变量是不可行的。
                //        break;
                //    }
                //    catch (System.Exception ex) {
                //        Console.WriteLine("您的输入有误,请重新输入");
                //        str = Console.ReadLine();
                //    }
                //  }
                //Console.WriteLine("您输入的数字是" + number)
            #endregion
               int  returnNumber=MyFirstMehod();//过程方式里的代码被MyFirstMethod所替代了.
               Console.WriteLine("您刚才输入的是" + returnNumber);
    
                int returnNumber1= MyFirstMehod(100);
                Console.WriteLine("您刚刚输入的是" + returnNumber1);
     
                int returnNumber2= MyFirstMehod(15, 65);
                Console.WriteLine("您刚刚输入的是" + returnNumber2);
                Console.ReadKey();
              }
        }
    }
    View Code

    题二:

    老师的代码:

    namespace 老师的代码_练习题2
    { 
        ///实现输入年份,月份,输出该月的天数学;
        class Program
        {
            static int ReadInt(string str)
            {
                return ReadInt(str,int.MinValue,int.MaxValue);
            }
    
            static int ReadInt(string str, int max)
            {
                return ReadInt(str,int.MinValue, max);
            }
            static int ReadInt(string str, int min,int max)
            {
                int inputNumber;
                do 
                {
                    Console.WriteLine(str);
                    try
                    {
                        inputNumber = Convert.ToInt32(Console.ReadLine());
                        if (inputNumber>min && inputNumber<max)
                        {
                            break;
                        } 
                        else
                        {
                            Console.Write("您的输入有误,");
                        }
                    }
                    catch 
                    {
                        Console.Write("您的输入有误,");
                    }
                    
                } while (true);
                return inputNumber;
            }
    
            static int GetDays(int year,int month)
            {
                if (month==2)
                {
                    if (year%400==0 && year%100!=0 || year%4==0)
                    {
                        return 29;
                    }
                    return 28;
                }
                else
                {
                    switch (month)
                    {
                        case 2 :
                        case 4:
                        case 6:
                        case 9:
                        case 11:
                        return 30;
                        default:
                        return 31;
                    }
                }
            }
    
            static void Main(string[] args)
            {
                int year = ReadInt("请输入年份", 2012);
                int month = ReadInt("请输入月份", 0, 13);
                Console.WriteLine(GetDays(year, month));
                Console.ReadKey();
            }
        }
    }
    View Code

     

    我叫小小菜,想要成为一棵大大包心菜.
  • 相关阅读:
    poj 2676 Suduku (dfs)
    poj 1562 Oil Deposits (dfs)
    poj 2907 Collecting Beepers (dfs)
    poj 1655 Balancing Act (树形dfs)
    poj 3411 Paid Roads (dfs)
    hdu 2896 病毒侵袭 (AC)
    hdu 3065 病毒侵袭持续中 (AC)
    poj 2251 Dungeon Master (bfs)
    java中debug使用
    Swing入门级小项目总结
  • 原文地址:https://www.cnblogs.com/tobecabbage/p/3432429.html
Copyright © 2011-2022 走看看