1 static void Main(string[] args) 2 { 3 //把24小时换成,12小时 4 int hour = 14; 5 string ap = ""; 6 ap = hour > 12 ? "pm" : "am"; 7 hour =hour >12?hour-12:hour ; 8 Console .WriteLine (hour+pm ); 9 10 11 12 13 14 }
显示结果:2pm
练习2:
1 static void Main(string[] args)
2 {
3 //把14点,换成2点
4 int hour = 14;
5 hour = hour> 12 ? hour - 12 : hour; //hour>12? 14>12成立 就走hour-12 结果是2
6 Console.WriteLine(hour);
7
8 }
显示结果:
练习3:
1 static void Main(string[] args)
2 {
3 //把14点,换成2点 ,2点为pm
4 int hour = 14;
5 string shangwuxiawu = "";
6 shangwuxiawu = hour > 12 ? "pm" : "am";
7 hour = hour> 12 ? hour - 12 : hour;//hour>12? 14>12成立 就走hour-12 结果是2
8 Console.WriteLine(hour+shangwuxiawu );
9
10 }
显示结果:
练习4:
1 static void Main(string[] args)
2 {
3 string shour="";
4 int hour = 0;
5 string ap = "";
6 string wenhou = "";
7 //输入
8 Console.Write("请输入当前小时:");
9 shour = Console.ReadLine();
10 //计算
11 hour = Convert.ToInt32(shour); //类型转换
12 wenhou = hour>=0&&hour<6?"晚上没有睡吗?":(hour>=6&&hour<11?"上午好!":(hour >=11&&hour<14?"中午好":(hou r>=14&&hour<18?"下午好":(hour>=18&&hour<22?"晚上好":(hour>=22&&hour<=23?"太晚了,早点休息":"时间不对啊")))));
13 ap = hour > 12 ? "PM" : "AM";
14 hour = hour > 12 ? hour - 12 : hour; //时制转换
15
16 //输出
17 Console.WriteLine("当前时间是:{0}{1}
{2}",hour,ap,wenhou);
18 }
显示结果:
练习5:
1 static void Main(string[] args)
2 {
3 string sex = "";
4 int age;
5 string jieGuo = "";
6 //输入
7 Console.Write("性别:");
8 sex = Console.ReadLine();
9 Console.Write("年龄:");
10 age = Convert.ToInt32(Console.ReadLine());
11
12 //运算
13 jieGuo = sex=="女"?"回家看门去":(age>=18&&age<=60?"抓去当兵":(age<18?"回家吃奶去":"回家养老吧"));
14
15
16 //输出
17 Console.WriteLine(jieGuo);
18 }
显示的结果:
练习6:
1 static void Main(string[] args) 2 { 3 string fangzi = "0"; 4 string piaozi = "0"; 5 string nengli = "0"; 6 string jieGuo = "0"; 7 //输入 8 Console.Write("姑娘:有房子吗?"); 9 fangzi = Console.ReadLine(); 10 Console.Write("姑娘:有钱吗?"); 11 piaozi = Console.ReadLine(); 12 Console.Write("姑娘:有能力吗?"); 13 nengli = Console.ReadLine(); 14 //运算 15 jieGuo = fangzi == "有" ? "咱们结婚吧" : (piaozi == "有" ? "买子结婚吧" : (nengli == "有" ? "快点赚钱买房子结婚吧" : "去学IT吧")); 16 17 18 //输出 19 Console.WriteLine(jieGuo); 20 } 21 static void Main9090(string[] args) 22 { 23 string sex = ""; 24 int age; 25 string jieGuo = ""; 26 //输入 27 Console.Write("性别:"); 28 sex = Console.ReadLine(); 29 Console.Write("年龄:"); 30 age = Convert.ToInt32(Console.ReadLine()); 31 32 //运算 33 jieGuo = sex=="女"?"回家看门去":(age>=18&&age<=60?"抓去当兵":(age<18?"回家吃奶去":"回家养老吧")); 34 35 36 //输出 37 Console.WriteLine(jieGuo); 38 }
显示的结果: