📜  嵌套条件运算符的C#程序

📅  最后修改于: 2021-05-29 18:02:06             🧑  作者: Mango

内部条件运算符可以在任何块中使用,它显示为(a> b)?((a> c)?a:c):( b> c?b:c)。在这里,我们输入三个数字,并使用嵌套条件运算符找到最大的数字。

句法:

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

通过上述条件运算符,它会逐个检查条件,如果为true,则执行“ true_block”,否则为“ false_block”,如果首先为false,则将执行“ false_block_outer”。

例子:

Input: Enter first number : 23
       Enter second number: 45
       Enter third number : 87

// check by using this method 
// (a>b)?((a>c)?a:c):(b>c?b:c);
Output: Largest number is 87

范例1:

C#
// C# program to illustrate example of
// nested conditional operator
using System;
using System.IO;
using System.Text;
  
namespace Geeks {
class GFG {
    
    // Main Method
    static void Main(string[] args)
    {
        // to find largest of three  numbers
        int a;
        int b;
        int c;
  
        // to 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());
  
        // to find 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();
    }
}
}


C#
// C# program to illustrate example of
// nested conditional operator
using System;
using System.IO;
  
class GFG {
    
    // Main Method
    static void Main(string[] args)
    {
        // to find largest of three  numbers
        int a;
        int b;
        int c;
  
        // to 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());
  
        // to find largest number
        int large = (a > b) ? ((a > c) ? a : c) : (b > c ? b : c);
  
        Console.WriteLine("Largest number is {0}", large);
    }
}


输入:

Enter first number : 23
Enter second number: 45
Enter third number : 87

输出:

Enter first number : 23
Enter second number: 45
Enter third number : 87
Largest number is 87

范例2:

C#

// C# program to illustrate example of
// nested conditional operator
using System;
using System.IO;
  
class GFG {
    
    // Main Method
    static void Main(string[] args)
    {
        // to find largest of three  numbers
        int a;
        int b;
        int c;
  
        // to 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());
  
        // to find largest number
        int large = (a > b) ? ((a > c) ? a : c) : (b > c ? b : c);
  
        Console.WriteLine("Largest number is {0}", large);
    }
}

输入:

Enter first number : 365
Enter second number: 123
Enter third number : 638

输出:

Enter first number : 365
Enter second number: 123
Enter third number : 638
Largest number is 638