C#方法(函数)

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace _04.方法_函数_
{
class Program
{
static void Main(string[] args)
{
//求2个整数的最大值
//int max = Program.GetMax(1, 2);
//Console.WriteLine("最大值为{0}", max);
//Console.ReadKey(); //判断闰年
//bool b = IsRun(2100);
//Console.WriteLine(b);
//Console.ReadKey(); //输入数字跳出循环,否则继续
//Console.WriteLine("请输入一个数字");
//string input = Console.ReadLine();
//int number = GetNum(input);
//Console.WriteLine(number);
//Console.ReadKey(); //输入yes\no,否则继续
//Console.WriteLine("请输入yes\no");
//string input = Console.ReadLine();
//string result = IsYesOrNo(input);
//Console.WriteLine(result);
//Console.ReadKey(); //求数组的和
//int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
//int result = GetSum(nums);
//Console.WriteLine(result);
//Console.ReadKey(); //求数组的最大、最小、总和、平均值
//int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
//int max = 0;
//int min = 0;
//int sum = 0;
//int avg = 0;
//Test(nums,out max,out min,out sum,out avg);
//Console.WriteLine(max);
//Console.WriteLine(min);
//Console.WriteLine(sum);
//Console.WriteLine(avg);
//Console.ReadKey(); //判断登录条件
//Console.WriteLine("username");
//string username = Console.ReadLine();
//Console.WriteLine("password");
//string password = Console.ReadLine();
//string msg;
//bool b = IsLogin(username, password, out msg);
//Console.WriteLine(b);
//Console.WriteLine(msg);
//Console.ReadKey(); //交换2个int型的整数
//int n1 = 10;
//int n2 = 20;
//Change(ref n1, ref n2);
//Console.WriteLine("{0}, {1}", n1, n2);
//Console.ReadKey(); //输入姓名、学号、成绩,计算总成绩
////int[] s = { 99, 99, 99 };
//Score("张三", 101, 99, 99, 99);
//Console.ReadKey(); //方法递归
//TellStory();
//Console.ReadKey(); //方法综合练习6.7.12 //求一个字符串数组中最长的元素
//string[] names = { "马云", "罗振宇", "科比布莱恩特", "扎克伯格"};
//Console.WriteLine(GetLongStr(names));
//Console.ReadKey();
} #region 2个数的最大值
/// <summary>
/// 求2个数的最大值
/// </summary>
/// <param name="n1">第一个整数</param>
/// <param name="n2">第二个整数</param>
/// <returns>返回最大值</returns>
public static int GetMax(int n1, int n2)
{
return n1 > n2 ? n1 : n2;
}
#endregion #region 判断闰年
/// <summary>
/// 判断给出的年份是否为闰年
/// </summary>
/// <param name="year">年份</param>
/// <returns>bool</returns>
public static bool IsRun(int year)
{
bool b = (year % == ) || (year % == && year % != );
return b;
}
#endregion #region 判断用户的输入是否为数字
/// <summary>
/// 判断用户的输入是否为数字
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static int GetNum(string s)
{
while (true)
{
try
{
int number = Convert.ToInt32(s);
return number;
}
catch
{
Console.WriteLine("输入有误");
s = Console.ReadLine();
}
}
}
#endregion #region yes\no跳出循环
/// <summary>
/// 输入yes\no
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string IsYesOrNo(string input)
{
while (true)
{
if (input == "yes" || input == "no")
{
return input;
}
else
{
Console.WriteLine("请重新输入");
input = Console.ReadLine();
}
}
}
#endregion #region 求数组的和
/// <summary>
/// 求数组的和
/// </summary>
/// <param name="numbers"></param>
/// <returns></returns>
public static int GetSum(int[] numbers)
{
int sum = ;
for (int i = ; i < numbers.Length; i++)
{
sum += numbers[i];
}
return sum;
}
#endregion #region 返回数组的最大、最小、总和、平均值
/// <summary>
/// 返回数组的最大、最小、总和、平均值
/// </summary>
/// <param name="nums">数组</param>
/// <param name="max">多于反悔的最大值</param>
/// <param name="min">多于反悔的最小值</param>
/// <param name="sum">多于反悔的总和</param>
/// <param name="avg">多于反悔的平均值</param>
public static void Test(int[] nums, out int max, out int min, out int sum, out int avg)
{
max = nums[];
min = nums[];
sum = ;
for (int i = ; i < nums.Length; i++)
{
if (nums[i] > max)
{
max = nums[i];
}
if (nums[i] < min)
{
min = nums[i];
}
sum += nums[i];
}
avg = sum / nums.Length;
}
#endregion #region 判断登录条件
/// <summary>
/// 判断登录条件
/// </summary>
/// <param name="uid">username</param>
/// <param name="pwd">password</param>
/// <param name="msg">错误信息</param>
/// <returns></returns>
public static bool IsLogin(string uid, string pwd, out string msg)
{
if (uid == "admin" && pwd == "")
{
msg = "登录成功";
return true;
}
else if (uid == "admin")
{
msg = "密码错误";
return false;
}
else if (pwd == "")
{
msg = "用户名错误";
return false;
}
else
{
msg = "全部错误";
return false;
}
}
#endregion #region 交换2个int型的整数
/// <summary>
/// 交换2个int型的整数
/// </summary>
/// <param name="n1">n1</param>
/// <param name="n2">n2</param>
public static void Change(ref int n1, ref int n2)
{
int temp = n1;
n1 = n2;
n2 = temp;
}
#endregion #region 输入姓名、学号、成绩,计算总成绩
/// <summary>
/// 输入姓名、学号、成绩,计算总成绩
/// </summary>
/// <param name="name">姓名</param>
/// <param name="id">学号</param>
/// <param name="score">成绩数组</param>
public static void Score(string name, int id, params int[] score)
{
int sum = ;
for (int i = ; i < score.Length; i++)
{
sum += score[i];
}
Console.WriteLine("{0}的总成绩为{1},学号{2}", name, sum, id);
}
#endregion #region 方法递归
/// <summary>
/// 方法递归
/// </summary>
public static int i = ;
public static void TellStory()
{
Console.WriteLine("从前有座庙");
Console.WriteLine("庙里有个老和尚和小和尚");
Console.WriteLine("有一天,老和尚对小和尚说:");
i++;
if (i > )
{
return;
}
TellStory();
}
#endregion #region 计算字符串数组中的最大值
/// <summary>
/// 计算字符串数组中的最大值
/// </summary>
/// <param name="s">数组</param>
/// <returns>最大值</returns>
public static string GetLongStr(string[] s)
{
string max = s[];
for (int i = ; i < s.Length; i++)
{
if (s[i].Length > max.Length)
{
max = s[i];
}
}
return max;
}
#endregion
}
}
  
上一篇:RBAC在thinkphp中有Auth类 可以很好的实现权限控制


下一篇:期待已久的2013年度最佳 jQuery 插件揭晓