zoukankan      html  css  js  c++  java
  • Net基础篇_学习笔记_第十天_方法_方法的练习

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace 方法001
     8 {
     9     class Program
    10     { 
    11         //写一个方法,判断一个年份是否为闰年
    12 
    13         static void Main(string[] args)
    14         {
    15             int a1=8;
    16             int a2=20;
    17             int max=GetMax(a1, a2);
    18             Console.WriteLine(max);
    19             Console.ReadKey();
    20           
    21         }
    22         /// <summary>
    23         /// 计算两个整数之间的最大值,并且返回最大值
    24         /// </summary>
    25         /// <param name="n1">第一个参数</param>
    26         /// <param name="n2">第二个参数</param>
    27         /// <returns>返回的最大值</returns>
    28         public static int GetMax(int n1,int n2)
    29         {
    30             int max=n1 > n2 ? n1 : n2;
    31             return max;
    32         }
    33     }
    34 }

    n1与n2为形参,a1与a2为实参。无论形参还是实参,都在内存里开辟了空间。

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace 方法001
     8 {
     9     class Program
    10     { 
    11         //读取输入的整数 多次调用(如果用户输入的是数字则返回,否则请用户重新输入)
    12           
    13 
    14         static void Main(string[] args)
    15         {
    16             Console.WriteLine("请输入一个数字:");
    17             string input = Console.ReadLine();
    18             int numb02 = GetNum(input);
    19             Console.WriteLine(numb02);
    20         }
    21         /// <summary>
    22         /// 这个方法需要判断用户的输入是否为数字,如果是数字返回,如果不是数字,请用户重新输入
    23         /// </summary>
    24         /// <returns></returns>
    25         public static int GetNum(String s)
    26         {
    27             while (true)
    28             {
    29                 try
    30                 {
    31                     int num = Convert.ToInt32(s);
    32                     return num;
    33                     
    34                 }
    35                 catch
    36                 {
    37                     Console.WriteLine("请重新输入");
    38                     s = Console.ReadLine();
    39                 }        
    40             }
    41         }
    42     }
    43 }

     方法的功能一定要单一,方法中最忌讳出现提示用户输入的字眼。

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace 方法001
     8 {
     9     class Program
    10     { 
    11         //读取输入的整数 多次调用(如果用户输入的是数字则返回,否则请用户重新输入)
    12           
    13 
    14         static void Main(string[] args)
    15         {
    16             Console.WriteLine("请输入yes/no?");
    17             string anwer = Console.ReadLine();
    18             string result = IsYesOrNo(anwer);
    19             Console.WriteLine("您选择的答案为:{0}",result);
    20             Console.ReadKey();
    21         }
    22         /// <summary>
    23         /// 限定用户只能输入yes/no,并且返回
    24         /// </summary>
    25         /// <param name="input">用户的输入</param>
    26         /// <returns>返回yes/no</returns>
    27         public static string IsYesOrNo(String input)
    28         {
    29             while (true)
    30             {
    31                 if (input == "yes" || input == "no")
    32                 {
    33                     return input;
    34                 }
    35                 else
    36                 {
    37                     Console.WriteLine("您输入的格式有误(yes/no),请重新输入");
    38                     input=Console.ReadLine();
    39                 }
    40             }
    41         }
    42     }
    43 }
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace 方法001
     8 {
     9     class Program
    10     {
    11         //读取输入的整数 多次调用(如果用户输入的是数字则返回,否则请用户重新输入)
    12         static void Main(string[] args)
    13         {
    14             int[] nums = { 5, 12, 515, 515, 51, 155 };
    15             int sums = GetSum(nums);
    16             Console.WriteLine("您输入的数组的总和为{0}",sums);
    17             Console.ReadKey();
    18         }
    19            /// <summary>
    20            /// 计算一个整数数组的总和
    21            /// </summary>
    22            /// <param name="numbers">有求数组总和的数组</param>
    23            /// <returns>返回这个数组的总和</returns>
    24         public static int GetSum(int[] numbers)
    25         {
    26             int sum = 0;
    27             for (int i = 0; i < numbers.Length; i++)
    28             {
    29                 sum += numbers[i];
    30             }
    31             return sum;
    32         }
    33     }
    34 }
  • 相关阅读:
    delphi 文件的读取(二进制文件和文本文件)
    delphi中用代码实现注册Ocx和Dll(有点怪异,使用CallWindowProc来调用指定函数DllRegisterServer)
    delphi 控件大全(确实很全)
    C#编写Windows 服务
    SSD Buffer Pool Extension
    .NET Web开发技术简单整理
    Python数据库访问之SQLite3、Mysql
    infiniband学习总结
    模板引擎开发3自定义标签的处理
    CSS选择器、优先级与匹配原理
  • 原文地址:https://www.cnblogs.com/NBOWeb/p/7453194.html
Copyright © 2011-2022 走看看