if 条件语法格式
if(布尔表达式){
表达式为真时要执行的代码;
}
📕⽰例1
有⼀个游乐场,只有年龄不⼤于16岁的⻘少年可以进⼊,判断输⼊的年龄是否符合条件。
c# int age = Convert.ToInt32(Console.ReadLine());
//if语句
if (age<=16) {
//当满足条件的时候会执行
Console.WriteLine("可以进入");
}
else
{
Console.WriteLine("年龄太大,不可以进");
}
📕⽰例2
让⽤户输⼊⼀个整数,判断是奇数还是偶数,并打印出来。
c# int number = Convert.ToInt32(Console.ReadLine());
//
if( number%2==1)
{
Console.WriteLine("奇数");
}
else
{
Console.WriteLine("偶数");
}
if else 语法格式
if(布尔表达式){
表达式为真时要执行的代码;
}else{
表达式为假时要执行的代码;
}
📕⽰例3
商场举办了送礼活动,年龄在18岁到30岁才可以参与活动,年龄是奇数才可以获得奖品。
c# int age = Convert.ToInt32(Console.ReadLine());
if( age>=18 && age<=30 && age%2==1)
{
Console.WriteLine("获得奖品");
}
if(age >= 18 && age <= 30)
{
Console.WriteLine("可以参加活动");
if(age % 2 == 1)
{
Console.WriteLine("获得奖品");
}
else
{
Console.WriteLine("没有获得奖品");
}
}
else
{
Console.WriteLine("不可以参加活动");
}
📕⽰例4
输⼊考试成绩(0-100),
如果90-100,评级为A
如果70-89,评级为B
如果60-69,评级为C
如果⼩于60,评级为D,
根据输⼊的成绩,输出评级
c# int score = Convert.ToInt32(Console.ReadLine());
// if if else if else if
方法1:
if (score >= 90)
{
Console.WriteLine("A");
} else if (score>=70)
{
Console.WriteLine("B");
} else if (score >= 60)
{
Console.WriteLine("C");
}else
{
Console.WriteLine("D");
}
方法2:
if (score >= 90)
{
Console.WriteLine("A");
}
if(score>=70 && score <= 89)
{
Console.WriteLine("B");
}
if (score >= 60 && score <= 69)
{
Console.WriteLine("C");
}
if (score < 60)
{
Console.WriteLine("D");
}
if else if 语法格式
if(布尔表达式 1){
布尔表达式 1 为真时要执行的代码;
}else if(布尔表达式 2){
布尔表达式 2 为真时要执行的代码;
}else if(布尔表达式 3){
布尔表达式 3 为真时要执行的代码;
}
...
else{
当所有布尔表达式都为假时要执行的代码;
}
📕⽰例5
输⼊平⾯直⾓坐标系中的⼀个坐标值,判断这个点是位于哪⼀个象限、原点或坐标轴上。
c# int x = Convert.ToInt32(Console.ReadLine());
int y = Convert.ToInt32(Console.ReadLine());
if(x>0 && y > 0)
{
Console.WriteLine("位于第一象限");
}else if (x<0&&y>0)
{
Console.WriteLine("位于第二象限");
}else if (x<0&&y<0)
{
Console.WriteLine("位于第三象限");
}else if (x>0&&y<0)
{
Console.WriteLine("位于第四象限");
}else if (0==x && y != 0)
{
Console.WriteLine("位于y轴");
}else if (0 == y && x != 0)
{
Console.WriteLine("位于x轴");
}
else
{
Console.WriteLine("位于原点");
}
📕⽰例6
输⼊三⾓形的三条边 a 、b 和c,判断是否可以组成三⾓形。
c# int a = Convert.ToInt32(Console.ReadLine());
int b = Convert.ToInt32(Console.ReadLine());
int c = Convert.ToInt32(Console.ReadLine());
if( a+b>c && a+c>b && b + c > a)
{
Console.WriteLine("可以组成三角形");
}
else
{
Console.WriteLine("不可以组成三角形");
}
📕⽰例7
输⼊⼀个年份,判断该年是不是闰年。是的话输出Yes,不是的话输出No。
c# int year = Convert.ToInt32(Console.ReadLine());
if( (year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
Console.WriteLine("是闰年");
}
else
{
Console.WriteLine("不是闰年");
}
📕⽰例8
输⼊⼀个⾮零整数,判断是正数还是负数,并输出它的绝对值。
c# int number = Convert.ToInt32(Console.ReadLine());
if (number < 0)
{
Console.WriteLine(0 - number);
}
else
{
Console.WriteLine(number);
}
📕⽰例9
最⼤值:输⼊三个整数,求这个三个数中最⼤值的平⽅。
c# int a = Convert.ToInt32(Console.ReadLine());
int b = Convert.ToInt32(Console.ReadLine());
int c = Convert.ToInt32(Console.ReadLine());
int max = a;
if (b > max)
{
max = b;
}
if (c > max)
{
max = c;
}
Console.WriteLine(c*c);
📕⽰例10
任意给出两个⼤写英⽂字⺟,⽐较他们的⼤⼩。规定 A、B、C、....Z依次从⼩到⼤。
c# char a = Convert.ToChar(Console.ReadLine());
char b = Convert.ToChar(Console.ReadLine());
if( a>b)
{
Console.WriteLine("{0}>{1}", a, b);
}
else
{
Console.WriteLine("{0}<{1}", a, b);
}
📕⽰例11 体能测试!每个⼈都需要测试两项项⽬。请输⼊你的性别和抽到的号码,输出你的测试项⽬。
样例输⼊ M 3 输出1000m 跳远
样例输⼊ F 8 输出800m 仰卧起坐
c# char sex = Convert.ToChar(Console.ReadLine());
int number = Convert.ToInt32(Console.ReadLine());
if (sex == 'F')
{
//Console.WriteLine("800M长跑");
string project = "800M长跑";
if ( number%2==1)
//Console.WriteLine("跳绳");
project += " 跳绳";
else
//Console.WriteLine("仰卧起坐");
project += " 仰卧起坐";
Console.WriteLine(project);
}
else
{
Console.WriteLine("1000M长跑");
if (number % 2 == 1)
{
Console.WriteLine("跳远");
}
else
{
Console.WriteLine("俯卧撑");
}
}
注
分⽀语句只有⼀⾏的话,可以省略{}
c# int x = 5, a = 0, b = 0;
if (x == a + b) Console.WriteLine("****");
else Console.WriteLine("####");