您的位置:首页 > 其它

循环语句(3)

2016-03-12 16:46 211 查看
今天还是讲的循环语句,好费脑子。冒烟了快。老师讲的时候挺明白,自己做的时候就不会了。555

今天讲了个好玩的例子:

Console.WriteLine("你爱不爱我");

for (int i=1;i<=4 ;i++ )
{
string a = Console.ReadLine();//一开始,把a放在for外面了,循环时出错,直接显示i==1,2,3,4时的语句。应该放在循环里接收语句。
if (a == "爱")
{
Console.WriteLine("恩,我也爱你。");
System.Threading.Thread.Sleep(2000);
Console.WriteLine("从此白雪公主和匹诺曹过上了幸福快乐的生活。");
break;
}
else
{
if (i == 1)
{
Console.WriteLine("你到底爱不爱我!");//执行完后接string a=……
}
if (i == 2)
{
Console.WriteLine("问你呢!你到底爱不爱我!");
}
if (i == 3)
{
Console.WriteLine("快回答!说你爱我!");
}
if(i==4)
{
Console.WriteLine("滚!");
}
}
}
Console.ReadLine();


判断日期格式是否正确:

try
{
Console.Write("年份;");
for (; ; )
{
int n = int.Parse(Console.ReadLine());
if (n <= 9999 && n >= 0)
{
Console.Write("月份:");
for (; ; )
{
int y = int.Parse(Console.ReadLine());
if (y >= 1 && y <= 12)
{
Console.Write("日期:");
for (; ; )
{
int r = int.Parse(Console.ReadLine());
if (y == 1 || y == 3 || y == 5 || y == 7 || y == 8 || y == 10 || y == 12)
{
if (r <= 31 && r >= 1)
{
Console.WriteLine("正确。");
break;
}
else
{
Console.WriteLine("错误,重输:");
}
break;
}
else if (y == 4 || y == 6 || y == 9 || y == 11)
{
if (r <= 30 && r >= 1)
{
Console.WriteLine("正确。");
break;
}
else
{
Console.WriteLine("错误,重输:");
}
break;
}
else if (y == 2)
{
if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
{
if (r >= 1 && r <= 29)
{
Console.WriteLine("正确");
break;//输入正确就跳出
}
else
{
Console.WriteLine("错,重输:");
}
}
else
{
if (r >= 1 && r <= 28)
{
Console.WriteLine("正确");
break;
}
else
{
Console.WriteLine("错,重输:");
}
}
break;
}
else
{
Console.WriteLine("您输入有误,重输:");
}
}
}
else
{
Console.WriteLine("您输入有误,重输:");
}
}
}
else
{
Console.WriteLine("您输入有误,重输:");
}
}
}
catch
{
Console.WriteLine("您输入有误");
}
Console.ReadLine();


//异常语句,try catch finall
try//保护执行里面的代码段,若其中一句有错误,直接跳转到下一段,不管下面的内容
{
Console.Write("一个整数:");
int a = int.Parse(Console.ReadLine());
}
catch//try有异常,直接执行,try无措,不执行。
{
Console.WriteLine("有误");

}
finally//不管有没有错,都要执行,相当于没有。
{
Console.WriteLine("谢谢使用。");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: