今天学了if 条件语句和作用域。作用域可以用一句话来概括:儿子可以用爹的所有东西,爹不可以用儿子的任何东西。If条件语句我用几个练习题来解释。
1、“请输入年份:”
判断是否是闰年,“xxxx年是闰年”,“xxxx年不是闰年”
namespace _2017_2_21
{
class Program
{
static void Main(string[] args)
{
Console.Write("请输入年份:");
int a=Convert.ToInt32(Console.ReadLine());
if(a>0&&a<9999&&a%4==0&&a%100!=0||a%400==0)
{
Console.Write(a+"年是闰年");
}
else
{
Console.Write(a+"年不是闰年");
}
Console.ReadLine();
}
}
}
2、“请输入您的分数:”
小于0,大于100,“输入的分数有误!”
大于0,小于10,“不及格!学渣!”
小于60,“不及格!继续努力!”
大于等于60,“恭喜你!及格了!”
大于等于90,“学霸!很厉害!”
namespace _2017_2_21
{
class Program
{
static void Main(string[] args)
{
Console.Write("请输入您的分数:");
Double a=Convert.ToDouble(Console.ReadLine());
if(a<0||a>100)
{
Console.WriteLine("输入的分数有误!");
}
if(a>0&&a<10)
{
Console.WriteLine("不及格!学渣!");
}
if(a>=10&&a<60)
{
Console.WriteLine("不及格!继续努力!");
}
if(a>=60&&a<90)
{
Console.WriteLine("恭喜你!及格了!");
}
if(a>=90&&a<=100)
{
Console.WriteLine("学霸!很厉害!");
}
Console.ReadLine();
}
}
}
3、猜拳
“请输入您的手势(石头/剪子/包袱):”
“用户赢了” “电脑赢了” “平局”
namespace _2017_2_21
{
class Program
{
static void Main(string[] args)
{
Console.Write("请输入您的手势(石头/剪子/包袱):");
String you=Console.ReadLine();
Random n = new Random();
int a = n.Next(1, 4);
Console.WriteLine(a);
if ((you == "石头" && a == 2) || (you == "剪子" && a == 3) || (you == "包袱" && a == 1))
{
Console.Write("你赢了");
}
else if((you=="石头"&&a==1)||(you=="剪子"&&a==2)||(you=="包袱"&&a==3))
{
Console.Write("平了");
}
else
{
Console.Write("你输了");
}
Console.ReadLine();
}
}
}
4、人工智能对话
如果说的是同一句话,不一定要回复同一句
namespace _2017_2_21
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请说话:");
String a = Console.ReadLine();
Random n = new Random();
int g = n.Next(0, 5);
if (g == 1)
{
Console.WriteLine("哈哈");
}
else if (g == 2)
{
Console.WriteLine("嘻嘻");
}
else if (g == 3)
{
Console.WriteLine("你好");
}
else if (g == 4)
{
Console.WriteLine("汪汪");
}
else if (g == 0)
{
Console.WriteLine("hello");
}
Console.ReadLine();
}
}
}
1、“请输入年份:”(1-9999)
“请输入月份:”(1-12)
“请输入日期:”(要判断大小月,判断闰年)
判断输入的时间日期是否正确
2、计算输入的时间是当前这一年的第几天
namespace _2017_2_22作业1
{
class Program
{
static void Main(string[] args)
{int year, month, date,num;
Console.WriteLine("请输入年份:(1-9999)");
year=Convert.ToInt32(Console.ReadLine());
if (year >= 1 && year <= 9999)
{
Console.WriteLine("请输入月份:");
month = Convert.ToInt32(Console.ReadLine());
if (month >= 1 && month <= 12)
{
Console.WriteLine("请输入日期:");
date = Convert.ToInt32(Console.ReadLine());
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12 && date >= 1 && date <= 31)
{
Console.WriteLine("您输入的日期格式正确");
if(month==1)
{
num = date;
Console.WriteLine("您输入的日期是当年的第"+num+"天");
}
if ((year >= 1 && year <= 9999 && year % 4 == 0 && year % 100 != 0 || year % 400 == 0) && month == 3)
{
num = 31 + 29 + date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
else if ((year >= 1 && year <= 9999 && year % 4 != 0 || year % 100 == 0 || year % 400 != 0) && month == 3)
{
num = 31 + 28 + date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
if ((year >= 1 && year <= 9999 && year % 4 == 0 && year % 100 != 0 || year % 400 == 0) && month == 5)
{
num = 31 + 29 + 31+30+date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
else if ((year >= 1 && year <= 9999 && year % 4 != 0 || year % 100 == 0 || year % 400 != 0) && month == 5)
{
num = 31 + 28 + 31 + 30 + date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
if ((year >= 1 && year <= 9999 && year % 4 == 0 && year % 100 != 0 || year % 400 == 0) && month == 7)
{
num = 31 + 29 + 31 + 30 +31+30+ date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
else if ((year >= 1 && year <= 9999 && year % 4 != 0 || year % 100 == 0 || year % 400 != 0) && month == 7)
{
num = 31 + 28 + 31 + 30 + 31 + 30 + date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
if ((year >= 1 && year <= 9999 && year % 4 == 0 && year % 100 != 0 || year % 400 == 0) && month == 8)
{
num = 31 + 29 + 31 + 30 + 31 + 30 +31+ date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
else if ((year >= 1 && year <= 9999 && year % 4 != 0 || year % 100 == 0 || year % 400 != 0) && month == 8)
{
num = 31 + 28 + 31 + 30 + 31 + 30 + 31 + date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
if ((year >= 1 && year <= 9999 && year % 4 == 0 && year % 100 != 0 || year % 400 == 0) && month == 10)
{
num = 31 + 29 + 31 + 30 + 31 + 30 +31+31+30+ date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
else if ((year >= 1 && year <= 9999 && year % 4 != 0 || year % 100 == 0 || year % 400 != 0) && month == 10)
{
num = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
if ((year >= 1 && year <= 9999 && year % 4 == 0 && year % 100 != 0 || year % 400 == 0) && month == 12)
{
num = 31 + 29 + 31 + 30 + 31 + 30+31+31+30+31+30+ date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
else if ((year >= 1 && year <= 9999 && year % 4 != 0 || year % 100 == 0 || year % 400 != 0) && month == 12)
{
num = 31 + 28 + 31 + 30 + 31 + 30+31+31+30+31+30+date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
}
else if ((month == 4 || month == 6 || month == 9 || month == 11) && date >= 1 && date <= 30)
{
Console.WriteLine("您输入的日期格式正确");
if ((year >= 1 && year <= 9999 && year % 4 == 0 && year % 100 != 0 || year % 400 == 0) && month == 4)
{
num = 31 + 29 + 31+ date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
else if ((year >= 1 && year <= 9999 && year % 4 != 0 || year % 100 == 0 || year % 400 != 0) && month == 4)
{
num = 31 + 28 + 31 + date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
if ((year >= 1 && year <= 9999 && year % 4 == 0 && year % 100 != 0 || year % 400 == 0) && month == 6)
{
num = 31 + 29 + 31 + 30 +31+date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
else if ((year >= 1 && year <= 9999 && year % 4 != 0 || year % 100 == 0 || year % 400 != 0) && month == 6)
{
num = 31 + 28 + 31 + 30 + 31 + date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
if ((year >= 1 && year <= 9999 && year % 4 == 0 && year % 100 != 0 || year % 400 == 0) && month == 9)
{
num = 31 + 29 + 31 + 30 + 31 + 30+31+31+ date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
else if ((year >= 1 && year <= 9999 && year % 4 != 0 || year % 100 == 0 || year % 400 != 0) && month == 9)
{
num = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
if ((year >= 1 && year <= 9999 && year % 4 == 0 && year % 100 != 0 || year % 400 == 0) && month ==11)
{
num = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30+31+ date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
else if ((year >= 1 && year <= 9999 && year % 4 != 0 || year % 100 == 0 || year % 400 != 0) && month == 11)
{
num = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
}
else if ((year >= 1 && year <= 9999 && year % 4 == 0 && year % 100 != 0 || year % 400 == 0) && month == 2 && date >= 1 && date <= 29)
{
Console.WriteLine("您输入的日期格式正确");
num = 31 + 29;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
else if ((year >= 1 && year <= 9999 && year % 4 != 0 || year % 100 == 0 || year % 400 != 0) && month == 2 && date >= 1 && date <= 28)
{
Console.WriteLine("您输入的日期格式正确");
num = 31 + 28;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
else
{
Console.WriteLine("您输入的日期格式不对");
}
}
else
{
Console.WriteLine("您输入的月份格式错误");
}
}
else
{
Console.WriteLine("您输入的年份格式错了");
}
Console.ReadLine();
}
}
}
3、标准体重
男士体重 = 身高 - 100 +-3
kg cm
女士体重 = 身高 - 110 +-3
namespace _2017_2_22标准体重
{
class Program
{
static void Main(string[] args)
{Console.WriteLine("请输入您的性别");
String sex=Console.ReadLine();
Console.WriteLine("请输入您的体重");
int weight = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入您的身高");
int height =Convert.ToInt32( Console.ReadLine());
if (sex == "男" && (height - weight - 100 >= -3 || height - weight - 100 <= 3))
{
Console.WriteLine("您的体重是标准体重");
}
else
{
if (height - weight - 100 < -3)
{
Console.WriteLine("您超重");
}
if (height - weight - 100>3)
{
Console.WriteLine("您偏瘦");
}
Console.ReadLine();
}
if (sex == "女" &&( height - weight - 110 >= -3 || height - weight - 110 <= 3))
{
Console.WriteLine("您的体重是标准体重");
}
else
{
if (height - weight - 110 < -3)
{
Console.WriteLine("您超重");
}
if (height - weight - 110 > 3)
{
Console.WriteLine("您偏瘦");
}
Console.ReadLine();
}
}
}
}
4、猜拳将用户的手势和电脑的手势输出出来
namespace _2017_2_22猜拳游戏显示手势
{
class Program
{
static void Main(string[] args)
{
Console.Write("请输入您的手势(石头/剪子/包袱):");
String you = Console.ReadLine();
Random n = new Random();
int a = n.Next(1, 4);
if (a==1)
{
Console.WriteLine("石头");
}
if (a == 2)
{
Console.WriteLine("剪子");
}
if (a == 3)
{
Console.WriteLine("包袱");
}
if ((you == "石头" && a == 2) || (you == "剪子" && a == 3) || (you == "包袱" && a == 1))
{
Console.Write("你赢了");
}
else if ((you == "石头" && a == 1) || (you == "剪子" && a == 2) || (you == "包袱" && a == 3))
{
Console.Write("平了");
}
else
{
Console.Write("你输了");
}
Console.ReadLine();
}
}
}
5、“请输入24小时制的时间:”
0-24 如果超出“时间输入有误”
namespace _2017_2_22时间24小时制转变成12小时制
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入24小时制的时间:");
int a =Convert.ToInt32( Console.ReadLine());
if(a>24||a<0)
{
Console.WriteLine("输入的时间有误");
}
if(a<=12&&a>6)
{
Console.WriteLine("上午"+a+"点");
}
if(a<=6)
{
Console.WriteLine("凌晨" + a + "点");
}
int b = a - 12;
if(a>12&&a<24)
{
Console.WriteLine("下午" + b + "点");
}
if(a==24)
{
Console.WriteLine("凌晨0点");
}
Console.ReadLine();
}
}
}