C#中只有一个三元运算符写法:
A?B:C 如果A的逻辑为True则返回B,否则返回C
View Code
1 using System; 2 3 using System.Collections.Generic; 4 5 using System.Linq; using System.Text; 6 7 namespace ConsoleApplication1 8 9 { 10 11 class Program 12 13 { 14 15 static void Main(string[] args) 16 17 { 18 19 //获得输入 20 21 Console.WriteLine("请输入num1"); 22 23 string num1 = Console.ReadLine(); 24 25 Console.WriteLine("请输入num2"); 26 27 string num2 = Console.ReadLine(); 28 29 //处理 30 31 int A = Convert.ToInt32(num1); 32 33 int B =Convert .ToInt32(num2); 34 35 int re =A +B ; 36 37 //输出 38 39 Console.WriteLine(A + B == re? "正确" : "错误"); 40 41 Console.ReadLine(); 42 43 } 44 45 } 46 47 }