#if #else c_C#程序演示多个if else语句的示例

#if #else c

Like other programming languages, multiple if else statement in C# is used to execute one code block written in multiple different blocks based on the condition. We can check multiple conditions by having their own code block sections and at a time only one code block section executes. If all conditions are false, then else block code section executes.

与其他编程语言一样,C#中的多个if else语句用于根据条件执行写入多个不同块中的一个代码块。 我们可以通过拥有自己的代码块部分来检查多个条件,并且一次只执行一个代码块部分。 如果所有条件都为假,则执行块代码部分。

Syntax:

句法:

    if(test_condition1){
	    //code section 1
    }
    else if(test_condition2){
    {
	    //code section 2
    }
    else if(test_condition3){
	    //code section 3
    }
    ...
    else{
	    //else code section
    }

Any number of tests can be checked, if any condition is true, statements written associated with that code section will be executes. If no one is true, then statements written in "else code section" will be executed.

可以检查任意数量的测试,如果条件为true ,则将执行与该代码段关联的编写的语句。 如果没有人为真 ,则将执行用“其他代码节”编写的语句。

1)C#示例1,用于多个if else语句 (1) C# example 1 for multiple if else statement)

Here, we are asking for an integer input – and checking whether input integer is positive value, negative value or a zero

在这里,我们要求输入整数-并检查输入整数是正值,负值还是零

// C# program to demonstrate example of 
// multiple if else statement 
using System;
using System.IO;
using System.Text;

namespace IncludeHelp
{
    class Test
    {
        // Main Method 
        static void Main(string[] args)
        {
            //input an integer number and check whether 
            //it is postive, negative or zero
            int number;
            
            Console.Write("Enter an integer number: ");
            number = Convert.ToInt32(Console.ReadLine());

            //checking conditions
            if (number > 0)
                Console.WriteLine("{0} is a positive number", number);
            else if (number < 0)
                Console.WriteLine("{0} is a negative number", number);
            else
                Console.WriteLine("{0} is a Zero", number);

            //hit ENTER to exit the program
            Console.ReadLine();
        }
    }
}

Output

输出量

Enter an integer number: -123
-123 is a negative number

2)多个if else语句的C#示例2 (2) C# example 2 for multiple if else statement)

Here, we are asking for a gender – and checking whether input gender is "Male", "Female" or "Unspecified gender".

在这里,我们要求提供性别-并检查输入的性别是“男”,“女”还是“未指定性别”。

// C# program to demonstrate example of 
// multiple if else statement 
using System;
using System.IO;
using System.Text;

namespace IncludeHelp
{
    class Test
    {
        // Main Method 
        static void Main(string[] args)
        {
            //input gender and check for "Male", "Female" or "Unspecied gender"
            string gender = "";

            Console.Write("Enter gender: ");
            gender = Console.ReadLine();

            if (gender.ToUpper() == "MALE")
                Console.WriteLine("He is male");
            else if (gender.ToUpper() == "FEMALE")
                Console.WriteLine("She is female");
            else
                Console.WriteLine("Unspecified gender");

            //hit ENTER to exit the program
            Console.ReadLine();
        }
    }
}

Output

输出量

First run:
Enter gender: male
He is male

Second run:
Enter gender: FEMale
She is female

Third run:
Enter gender: Don't know
Unspecified gender


翻译自: https://www.includehelp.com/dot-net/multiple-if-else-statement-example-in-c-sharp.aspx

#if #else c

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值