三元运算符 a?b:c;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入一个数:");
int num = int.Parse(Console.ReadLine());
//int.Parse用于将屏幕的语句转换为整型。
Console.WriteLine(num < 10 ? "The number is smaller than 10." : "The number is bigger than 10.");
Console.ReadKey();
}
}
}
分支语句总结:
if语句:特别适合判断一些连续的值,可与else或else if配合使用。
switch语句:特别适合判断一些离散的值。在使用的时候要注意每个case语句之后都必须有break语句,可配合default语句使用。
三元运算符:比较适用于简单的赋值语句,适用于判断有两个结果的情况,可读性较差。