您的位置:首页 > 编程语言 > C#

C#入门篇5-1:流程控制语句 if判断语句

2012-04-17 18:09 435 查看
#region if判断语句
public class C1
{
//判断命令行参数
public static int Fun1(string[] args)
{
//如果命令行参数小于一个,则显示程序正确用法,退出程序
if (args.Length < 1)
{
Console.WriteLine("Usage: ifApp char");
return 1;
}

//获得第一个命令行参数第一个字母
//把该字母赋给变量chLetter
char chLetter = args[0][0];

//如果字母大于等于字符'A'
if (chLetter >= 'A')
{
//同时,字母小于字符'Z'
//则该字母为大写字母
if (chLetter <= 'Z')
{
Console.WriteLine("{0} 是个大写字母", chLetter);
return 0;
}
}

//如果字母界与字符'a'和'z'之间
//则该字母为小写字母
if (chLetter >= 'a' && chLetter <= 'z')
{
Console.WriteLine("{0} 是个小写字母", chLetter);
return 0;
}

//如果字母为数字
if (Char.IsDigit(chLetter))
{
//IsDigit指示指定的unicode字符是否属于十进制数字类别
Console.WriteLine("{0} 是个数字", chLetter);
return 0;
}

//缺省地(以上条件都不符合),则该字母为特殊字符
Console.WriteLine("{0} 是个特殊字符", chLetter);
return 1;

//注意:该程序需要使用csc命令编译之后,在控制台下调用编译之后exe文件方可看见效果
}

//判断分数的等级
public static void Fun2()
{
lab:
Console.WriteLine("请输入您的得分:");
int Score = Convert.ToInt32(Console.ReadLine().Trim());

if (Score >= 0 && Score <= 100)
{
switch (Score / 10)
{
case 10:
Console.WriteLine("得分等级:满分");
break;
case 9:
Console.WriteLine("得分等级:A");
break;
case 8:
Console.WriteLine("得分等级:B");
break;
case 7:
Console.WriteLine("得分等级:C");
break;
case 6:
Console.WriteLine("得分等级:D");
break;
default:
Console.WriteLine("得分等级:E");
break;
}
}
else
{
Console.WriteLine("百分制计分");
}
goto lab;

}

//判断生肖
public static void Fun3()
{
Console.WriteLine("请输入年份:");
int Year = int.Parse(Console.ReadLine().Trim());
//求生肖需要对12求余数
if (Year % 12 == 0)
{
Console.WriteLine("猴");
}
else if (Year % 12 == 1)
{
Console.WriteLine("鸡");
}
else if (Year % 12 == 2)
{
Console.WriteLine("狗");
}
else if (Year % 12 == 3)
{
Console.WriteLine("猪");
}
else if (Year % 12 == 4)
{
Console.WriteLine("鼠");
}
else if (Year % 12 == 5)
{
Console.WriteLine("牛");
}
else if (Year % 12 == 6)
{
Console.WriteLine("虎");
}
else if (Year % 12 == 7)
{
Console.WriteLine("兔");
}
else if (Year % 12 == 8)
{
Console.WriteLine("龙");
}
else if (Year % 12 == 9)
{
Console.WriteLine("蛇");
}
else if (Year % 12 == 10)
{
Console.WriteLine("马");
}
else
{
Console.WriteLine("羊");
}
}

//验证十五位
public static void Fun4()
{
//写一个身份证判断的程序(判断位数和年月日)
lab:
Console.WriteLine("请输入省份证号码:");
string strIdCard = Console.ReadLine().Trim();

int length = strIdCard.Length;

//验证十五位或者是十八位

if (length == 18 || length == 15)
{
if (length == 18)
{
//截取年月日转换成整形数据

int year = int.Parse(strIdCard.Substring(6, 4));

int month = int.Parse(strIdCard.Substring(10, 2));

int day = int.Parse(strIdCard.Substring(12, 2));

Console.WriteLine("年:{0} 月:{1} 日:{2}", year, month, day);

#region 闰年的情况

if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
//闰年二月二十九天
if ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && (day >= 1 && day <= 31))
{
Console.WriteLine("身份证号正确");
}
else if ((month == 4 || month == 6 || month == 9 || month == 11) && (day >= 1 && day <= 30))
{
Console.WriteLine("身份证号正确");
}
if ((month == 2) && (day >= 1 && day <= 29))
{
Console.WriteLine("身份证号正确");
}
else { Console.WriteLine("错误的身份证号"); }
}
else
{
//闰年二月二十八天
if ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && (day >= 1 && day <= 31))
{
Console.WriteLine("身份证号正确");
}
else if ((month == 4 || month == 6 || month == 9 || month == 11) && (day >= 1 && day <= 30))
{
Console.WriteLine("身份证号正确");
}
else if ((month == 2) && (day >= 1 && day <= 28))
{
Console.WriteLine("身份证号正确");
}
else { Console.WriteLine("错误的身份证号"); }
}
goto lab;
#endregion
}
}
}

//互换变量
public static void Fun5()
{
//中间变量交换值
int intA = 5;
int intB = 3;
int temp = 0;

//如果intA>intB 就借助intC的值进行交换
if (intA > intB)
{
temp = intA;
intA = intB;
intB = temp;
}
Console.WriteLine("intA的值是:" + intA + "\n" + "intB的值是:" + intB);
}

//本例演示if判断验证QQ登录
public static void Fun6()
{
/*
* 假设有一组帐户:用户名:Guest 密码:123456,现模拟QQ登录验证。
* 要求:当用户名和密码都正确时,
* 提示"欢迎您,登录成功";
* 当用户名或者密码错误时,
* 提示"用户名或密码有误,请重新输入",
* 并且清空用户名和密码文本框;
* 如果累计错误次数达到3次就退出程序。
*/
int cnt = 0;//记录用户输入的次数

Console.WriteLine("请输入用户名:");
string strname = Console.ReadLine().Trim();

Console.WriteLine("请输入密码:");
string strpwd = Console.ReadLine().Trim();

//首先要验证输入的不能为空,在这个前提下再做进一步的验证

if (strname != "" && strpwd != "")
{
if (strpwd == "123456" && strname == "Guest")
{
Console.WriteLine("登录成功!");
}
else
{
if (strname != "Guest")
{
Console.WriteLine("用户名错误!");
}

if (strname == "Guest" && strpwd != "123456")
{
Console.WriteLine("密码错误!");
}
cnt++;

if (cnt == 3)
{
Console.WriteLine("退出应用程序");
//this.Close();//关闭窗体
//Application.Exit();//退出应用程序
}
}
}
else
{
Console.WriteLine("用户名和密码不能为空!");
}
}

//本例演示计算邮费的问题
public static void Fun7()
{
//计算邮费:100克以内(包括100克)0.5元收费,超出的部分按0.08元收费
//熟悉流程控制语句的不同写法
//变量的赋值是覆盖式的
//if 的流程控制作用,变量的变化

Console.WriteLine("请输入包裹重量:");

double weight = double.Parse(Console.ReadLine().Trim());
double fare;     //体会变量的特点

if (weight <= 100)
{
fare = weight * 0.5;
}
else
{
fare = weight * 0.5 + (weight - 100) * 0.08;
}
Console.WriteLine("此包裹费用为:" + fare + "元");

//可以这样
fare = weight * 0.5;
if (weight > 100)
{
fare = weight * 0.5 + (weight - 100) * 0.08;
}
Console.WriteLine("此包裹费用为:" + fare + "元");

//也可以这样
fare = weight * 0.5 + (weight - 100) * 0.08;
if (weight <= 100)
{
fare = weight * 0.5;
}
Console.WriteLine("此包裹费用为:" + fare + "元");
}

//if***else if语句
public static void Fun8()
{
/* 1 如果文本框为空, 则提示用户文本框的值不允许为空;
* 2 如果用户输入的是一个负数 则判断提示用户不允许输入负数;
* 3 如果用户输入的是大于50的数, 则判断提示用户只允许输入小于等于 50的自然数;
*/
lab1:
int num = int.MinValue;

double Result = 1; //定义double型的p存放结果

Console.WriteLine("请输入数字:");

string str = Console.ReadLine().Trim();
if (str == "")
{
Console.WriteLine("本文框不能空");
}
else
{
num = int.Parse(str);
if (num < 0)
{
Console.WriteLine("请输入正数");
}
else if (num > 50)
{
Console.WriteLine("只能是小于等于50的自然数");
}
else
{
for (int i = 1; i <= num; i++)
{
Result *= i;
}
Console.WriteLine(Result);
}
}
goto lab1;
}
}
#endregion
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: