c#如何嵌套第三方程序_C#程序演示嵌套条件运算符的示例

c#如何嵌套第三方程序

C# (or other programming languages also) allows to use a conditional/ternary operator within another conditional/ternary operator.

C#(或其他编程语言)还允许在另一个条件/三元运算符中使用条件/三元运算符。

Syntax:

句法:

    (logical_test1) ? 
        ((logical_test2)? True_block : false_block) : 
        false_block_outer;

If logical_test1 is true then logical_test2 will be checked, if it is true then "true_block" executes, else "false_block" executes, and if logical_test1 is false then "false_block_outer" will be executed.

如果logical_test1为true,则将检查logical_test2 ,如果为true,则将执行“ true_block” ,否则将执行“ false_block” ,如果logical_test1为false,则将执行“ false_block_outer”

Note: Inner conditional operator can be used in any block as per the requirement.

注意:根据条件,可以在任何块中使用内部条件运算符。

嵌套条件/三元运算符的C#示例 (C# example for nested conditional/ternary operator)

Here, we are asking to input three numbers and finding the largest number.

在这里,我们要求输入三个数字并找到最大的数字。

// C# program to demonstrate example of 
// nested conditional operator
using System;
using System.IO;
using System.Text;

namespace IncludeHelp
{
    class Test
    {
        // Main Method 
        static void Main(string[] args)
        {
            //finding largest of three  numbers 
            int a;
            int b;
            int c;

            //input numbers 
            Console.Write("Enter first number : ");
            a = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter second number: ");
            b = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter third number : ");
            c = Convert.ToInt32(Console.ReadLine());

            //finding largest number
            int large = (a>b)?((a>c)?a:c):(b>c?b:c);

            Console.WriteLine("Largest number is {0}", large);

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

Output

输出量

First run:
Enter first number : 30
Enter second number: 20
Enter third number : 10
Largest number is 30

Second run:
Enter first number : 10
Enter second number: 30
Enter third number : 20
Largest number is 30

Third run:
Enter first number : 10
Enter second number: 20
Enter third number : 30
Largest number is 30



翻译自: https://www.includehelp.com/dot-net/nested-conditional-operator-example-in-c-sharp.aspx

c#如何嵌套第三方程序

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值