语句
if语句
大体可以分一下几种:
小知识 生成一个随机数:
Random sss = new Random();
int a = sss.Next(100);
Console.WriteLine(a);
例子:
(一)根据月收入判断是否需要缴税
Console.Write("请输入你的月收入(元):");
double money = Convert.ToDouble(Console.ReadLine());
if (money >= 3500)
{
Console.WriteLine("请交税");
Console.WriteLine("谢谢合作!");
}
else
{
Console.WriteLine("继续努力");
Console.WriteLine("谢谢合作!");
}
(二)根据 房子,钱,能力 判断女方是否与男方结婚
try //避免输入非true/false而报错
{
Console.Write("你有房吗(true/false)?");
bool house = Convert.ToBoolean(Console.ReadLine());
if (house == true)
{
Console.WriteLine("哦我们结婚吧!!!!");
}
else
{
Console.Write("你有钱吗(true/false)?");
bool money = Convert.ToBoolean(Console.ReadLine());
if (money == true)
{
Console.WriteLine("先买房子再结婚!");
}
else
{
Console.Write("你有能力吗(true/false)?");
bool ability = Convert.ToBoolean(Console.ReadLine());
if (ability == true)
{
Console.WriteLine("骚年,先赚点钱吧!");
}
else
{
Console.WriteLine("滚蛋!!!!!!");
}
}
}
}
catch
{
Console.WriteLine("输入错误!");
}
(三)求ax^2+bx+c=0的根
try
{
Console.WriteLine("求ax^2+bx+c=0的根");
Console.Write("a=");
int a = Convert.ToInt32(Console.ReadLine());
Console.Write("b=");
int b = Convert.ToInt32(Console.ReadLine());
Console.Write("c=");
int c = Convert.ToInt32(Console.ReadLine());
if (a == 0)
{
Console.WriteLine("不是一元二次方程!");
}
else
{
int delta = b * b - 4 * a * c;
if (delta > 0)
{
Console.WriteLine("x1={0},x2={1}", (-b + Math.Sqrt(delta) / 2 * a), (-b - Math.Sqrt(delta) / 2 * a));
//Math.Sqrt() 即开方
}
else if (delta == 0)
{
Console.WriteLine("x=" + -b / 2 * a);
}
else
{
Console.WriteLine("方程无实根!");
}
}
}
catch
{
Console.WriteLine("输入不对");
}
(四)输入身高体重,判断健康状况
try
{
Console.WriteLine("输入身高体重,判断健康状况");
Console.Write("请输入性别:");
string sex = Console.ReadLine();
Console.Write("请输入身高:");
int height = Convert.ToInt32(Console.ReadLine());
Console.Write("请输入体重:");
int weight = Convert.ToInt32(Console.ReadLine());
if (sex == "男")
{
int num = height - 100 - weight;
if (num > 3)
{
Console.WriteLine("偏瘦");
}
else if (num >= -3 && num <= 3)
{
Console.WriteLine("正常");
}
else
{
Console.WriteLine("偏胖");
}
}
else if (sex == "女")
{
int num1 = height - 110 - weight;
if (num1 > 3)
{
Console.WriteLine("偏瘦");
}
else if (num1 >= -3 && num1 <= 3)
{
Console.WriteLine("正常");
}
else
{
Console.WriteLine("偏胖");
}
}
}
catch
{
Console.WriteLine("输入有误!");
}