函数的学习

1 判断闰年的方法

//写一个方法,判断一个年份是否是闰年.
Main
{
Console.WriteLine("请输入一个年份");
int year=Convert.Toint32(Console.ReadLine);
bool b=IsRun(year);
Console.WriteLine(b);
}
///判断用户输入的年份是否是闰年
public static bool IsRun(int yaer)
{
if((year%400==0)||(year%4==0&&year%100!=0))
{
return true;
}
else
{
return false;
}


2 方法练习

//1 读取输入的整数
//多次调用(如果用户输入的是数字,则返回,否则提示用户重新输入)
Main
{
Console.WriteLine("请输入一个数字");
string input=Console.ReadLine();
int s=IsNumber(input);
Console.WriteLine(s);
Console.ReadKey();
}
public static int IsNumber(string input)
{
while(true)
{
try{
int number=Convert.Toint32(input);
return number
   }
catch{
Console.WriteLine("你输入错误,请重新输入");
input=Console.ReadLine();
}
}
//2 还记得学循环时做的那道题吗?只允许用户输入y或n,请改成方法
            //这个方法做了什么事儿?
            //只能让用户输入yes或者no,只要不是,就重新输入
            //输入yes 看 输入no 不能看   

Main
{
Console.WriteLine("请输入yes或no");
string input=Console.ReadLine();
string result=IsInput(input);
Console.WriteLine(result);
Console.ReadKey();
}
public static string IsInput(string input)
{
While(true)
{
if(input=="yes"||input=="no")
{
return input
}
else
{
Console.WriteLine("输入错误,请重新输入")
input=Console.ReadLine();
}
Main
{
//4计算输入数组的和:int Sum(int[] values)
int[]nums={1,2,3,4,5,6,7,8,9,}
int sum=IsSum(nums);
Console.WriteLine(sum);
Console.ReadKey();
}
public static int ISSum(int[]nums)
{
for(i=0;i<nums.Length;i++)
{
sum+=nums{i};
}
return sum;
}










out参数

Main{
//分别的提示用户输入用户名和密码
//你写一个方法来判断用户输入的是否正确
//返回给用户一个登陆结果,并且还要单独的返回给用户一个登陆信息
 //如果用户名错误,除了返回登陆结果之外,还要返回一个 "用户名错误"
 //“密码错误”
Console.WriteLine("请输入用户名");
String name=Console.ReadLine();
Console.WriteLine("请输入密码");
string pwd=Console.ReadLine();
bool b=IsInput(name,pwd,out input);
Console.WriteLine("登录结果{0}",b);
Console.WriteLine("登录信息{0}"input);
Console.ReadKey();
}
///判断登录是否成功
///name 用户名
///pwd 密码
///多余返回的登录信息
public static bool IsInput(string name,string pwd,out string input)
{
if(name=="admin"&&pwd=="888888")
{
input="登陆成功";
return true;
else if(name!="admin")
{
input="输入用户名错误";
return false;
}
else if(pwd!="888888")
{
input="输入密码错误";
return false;
}
else
{
input="用户名密码错误";
return false;
}
}
}

ref参数

Main
{
//使用方法来交换两个int类型的变量
int n1=10;
int n2=20;
Exchange(ref n1,ref n2)
Console.WriteLine("交换后的值为{0},{1}",n1,n2);
Console.readKey();
}
///交换两个值
///交换前的值
public static int Exchange(ref int n1,ref int n2)
{
int temp=n1;
n1=n2;
n2=temp;
}

params参数

Main
{
//求任意长度数组的和 整数类型的
int[] nums={1,2,3,4,5,6,7,8,9,}
int sum=IsSums(1,2,3,4,5,6,,7,8,9,)
Console.WriteLine(sum)
Console.ReadKey();
int score=IsScore(80,90,70,);
Console.WriteLine("学生的总成绩为{0}",Score);
Console.ReadKey();
}
///计算学生成绩总值
/// 学生成绩数组
///返回成绩总和
public static int IsSums(params int[]s)
{
int sum=0;
for(i=0;i<s.Length;i++)
{
sum+=s[i];
return sum;
}
public staic int IsScore(params int[]score)
{
int sum=0;
for (i=0;i<score.Length;i++)
{
sum+=score;
return sum;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值