C#教程
C#控制语句
C#函数
C#数组
C#面向对象
C#命名空间和异常
C#文件IO
C#集合
C#多线程
C#其它

C# if-else

C# if-else

在 C# 编程中,if 语句用于测试条件。 C# 中有多种类型的 if 语句。
if 语句 if-else 语句 嵌套 if 语句 if-else-if 阶梯

C# IF 语句

C# if 语句测试条件。如果条件为真则执行。
语法:
if(condition){
//code to be executed
}
if statement in java

C# If Example

using System;    
public class IfExample
    {
       public static void Main(string[] args)
        {
            int num = 10;
            if (num % 2 == 0)
            {
                Console.WriteLine("It is even number");
            }
            
        }
    }
输出:
It is even number

C# IF-else 语句

C# if-else 语句也测试条件。如果条件为真,则执行 if 块,否则执行 else 块。
语法:
if(condition){
//code if condition is true
}else{
//code if condition is false
}
C# if-else statement

C# If-else 示例

using System;    
public class IfExample
    {
        public static void Main(string[] args)
        {
            int num = 11;
            if (num % 2 == 0)
            {
                Console.WriteLine("It is even number");
            }
            else
            {
                Console.WriteLine("It is odd number");
            }
            
        }
    }
输出:
It is odd number

C# If-else 示例: 使用来自用户的输入

在此示例中,我们使用 Console.ReadLine() 从用户获取输入方法。它返回字符串。对于数值,您需要使用 Convert.ToInt32() 方法将其转换为 int。
using System;    
public class IfExample
    {
       public static void Main(string[] args)
        {
            Console.WriteLine("Enter a number:");
            int num = Convert.ToInt32(Console.ReadLine());
            if (num % 2 == 0)
            {
                Console.WriteLine("It is even number");
            }
            else
            {
                Console.WriteLine("It is odd number");
            }
            
        }
    }
输出:
Enter a number:11
It is odd number
输出:
Enter a number:12
It is even number

C# IF-else-if 梯形图语句

C# if-else-if 梯形图语句从多个语句中执行一个条件。
语法:
if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are false
}
C# if-else-if statement

C# If else-if 示例

using System;    
public class IfExample
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Enter a number to check grade:");
            int num = Convert.ToInt32(Console.ReadLine());
            if (num <0 || num >100)
            {
                Console.WriteLine("wrong number");
            }
            else if(num >= 0 && num < 50){
                Console.WriteLine("Fail");
            }
            else if (num >= 50 && num < 60)
            {
                Console.WriteLine("D Grade");
            }
            else if (num >= 60 && num < 70)
            {
                Console.WriteLine("C Grade");
            }
            else if (num >= 70 && num < 80)
            {
                Console.WriteLine("B Grade");
            }
            else if (num >= 80 && num < 90)
            {
                Console.WriteLine("A Grade");
            }
            else if (num >= 90 && num <= 100)
            {
                Console.WriteLine("A+ Grade");
            }
        }
    }
输出:
Enter a number to check grade:66
C Grade
输出:
Enter a number to check grade:-2
wrong number
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4