今天学了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>&&a<&&a%==&&a%!=||a%==)
{
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<||a>)
{
Console.WriteLine("输入的分数有误!");
}
if(a>&&a<)
{
Console.WriteLine("不及格!学渣!");
}
if(a>=&&a<)
{
Console.WriteLine("不及格!继续努力!");
}
if(a>=&&a<)
{
Console.WriteLine("恭喜你!及格了!");
}
if(a>=&&a<=)
{
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(, );
Console.WriteLine(a); if ((you == "石头" && a == ) || (you == "剪子" && a == ) || (you == "包袱" && a == ))
{
Console.Write("你赢了");
}
else if((you=="石头"&&a==)||(you=="剪子"&&a==)||(you=="包袱"&&a==))
{
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(, );
if (g == )
{
Console.WriteLine("哈哈");
}
else if (g == )
{
Console.WriteLine("嘻嘻");
}
else if (g == )
{
Console.WriteLine("你好");
}
else if (g == )
{
Console.WriteLine("汪汪");
}
else if (g == )
{
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 >= && year <= )
{
Console.WriteLine("请输入月份:");
month = Convert.ToInt32(Console.ReadLine());
if (month >= && month <= )
{
Console.WriteLine("请输入日期:");
date = Convert.ToInt32(Console.ReadLine());
if (month == || month == || month == || month == || month == || month == || month == && date >= && date <= )
{
Console.WriteLine("您输入的日期格式正确");
if(month==)
{
num = date;
Console.WriteLine("您输入的日期是当年的第"+num+"天");
}
if ((year >= && year <= && year % == && year % != || year % == ) && month == )
{
num = + + date;
Console.WriteLine("您输入的日期是当年的第" + num + "天"); }
else if ((year >= && year <= && year % != || year % == || year % != ) && month == )
{
num = + + date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
if ((year >= && year <= && year % == && year % != || year % == ) && month == )
{
num = + + ++date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
else if ((year >= && year <= && year % != || year % == || year % != ) && month == )
{
num = + + + + date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
if ((year >= && year <= && year % == && year % != || year % == ) && month == )
{
num = + + + +++ date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
else if ((year >= && year <= && year % != || year % == || year % != ) && month == )
{
num = + + + + + + date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
} if ((year >= && year <= && year % == && year % != || year % == ) && month == )
{
num = + + + + + ++ date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
else if ((year >= && year <= && year % != || year % == || year % != ) && month == )
{
num = + + + + + + + date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
} if ((year >= && year <= && year % == && year % != || year % == ) && month == )
{
num = + + + + + ++++ date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
else if ((year >= && year <= && year % != || year % == || year % != ) && month == )
{
num = + + + + + + + + + date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
if ((year >= && year <= && year % == && year % != || year % == ) && month == )
{
num = + + + + + ++++++ date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
else if ((year >= && year <= && year % != || year % == || year % != ) && month == )
{
num = + + + + + ++++++date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
} else if ((month == || month == || month == || month == ) && date >= && date <= )
{
Console.WriteLine("您输入的日期格式正确");
if ((year >= && year <= && year % == && year % != || year % == ) && month == )
{
num = + + + date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
else if ((year >= && year <= && year % != || year % == || year % != ) && month == )
{
num = + + + date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
if ((year >= && year <= && year % == && year % != || year % == ) && month == )
{
num = + + + ++date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
else if ((year >= && year <= && year % != || year % == || year % != ) && month == )
{
num = + + + + + date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
if ((year >= && year <= && year % == && year % != || year % == ) && month == )
{
num = + + + + + +++ date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
else if ((year >= && year <= && year % != || year % == || year % != ) && month == )
{
num = + + + + + + + + date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
if ((year >= && year <= && year % == && year % != || year % == ) && month ==)
{
num = + + + + + + + + ++ date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
} else if ((year >= && year <= && year % != || year % == || year % != ) && month == )
{
num = + + + + + + + + + + date;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
}
else if ((year >= && year <= && year % == && year % != || year % == ) && month == && date >= && date <= )
{
Console.WriteLine("您输入的日期格式正确");
num = + ;
Console.WriteLine("您输入的日期是当年的第" + num + "天");
}
else if ((year >= && year <= && year % != || year % == || year % != ) && month == && date >= && date <= )
{
Console.WriteLine("您输入的日期格式正确");
num = + ;
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 - >= - || height - weight - <= ))
{
Console.WriteLine("您的体重是标准体重");
}
else
{
if (height - weight - < -)
{
Console.WriteLine("您超重");
}
if (height - weight - >)
{
Console.WriteLine("您偏瘦");
}
Console.ReadLine();
}
if (sex == "女" &&( height - weight - >= - || height - weight - <= ))
{
Console.WriteLine("您的体重是标准体重");
}
else
{
if (height - weight - < -)
{
Console.WriteLine("您超重");
}
if (height - weight - > )
{
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(, ); if (a==)
{
Console.WriteLine("石头");
}
if (a == )
{
Console.WriteLine("剪子");
}
if (a == )
{
Console.WriteLine("包袱");
} if ((you == "石头" && a == ) || (you == "剪子" && a == ) || (you == "包袱" && a == ))
{
Console.Write("你赢了");
}
else if ((you == "石头" && a == ) || (you == "剪子" && a == ) || (you == "包袱" && a == ))
{
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>||a<)
{
Console.WriteLine("输入的时间有误");
}
if(a<=&&a>)
{
Console.WriteLine("上午"+a+"点");
}
if(a<=)
{
Console.WriteLine("凌晨" + a + "点");
}
int b = a - ;
if(a>&&a<)
{
Console.WriteLine("下午" + b + "点");
}
if(a==)
{
Console.WriteLine("凌晨0点");
} Console.ReadLine();
}
}
}