练手
1 Console.Write("请输入第一个数:"); 2 string a = Console.ReadLine(); 3 int a1 = Convert.ToInt16(a); 4 5 Console.Write("请输入第二个数:"); 6 string b = Console.ReadLine(); 7 int b1 = Convert.ToInt16(b); 8 9 Console.Write("请输入+/-"); 10 string c = Console.ReadLine(); 11 12 int x1 = a1 + b1; 13 int y1 = a1 - b1; 14 string x = Convert.ToString(x1); 15 string y = Convert.ToString(y1); 16 17 18 19 string jia=(c=="+")?x:y; 20 Console.Write(jia); 21 22 23 Console.ReadLine();
1 Console.Write("请输入一个0~100的整数:"); 2 string a1 = Console.ReadLine(); 3 int a = Convert.ToInt16(a1); 4 string b = a < 50 || a == 66 ? "小" : "大"; 5 Console.Write(b); 6 7 8 9 Console.ReadLine();
1 Console.Write("请输入您的成绩(0~100):"); 2 string a = Console.ReadLine(); 3 double a1 = Convert.ToDouble(a); 4 5 string b = a1 >= 60 ? "及格了!" : "不及格,继续努力!"; 6 Console.Write(b); 7 8 Console.ReadLine();
1 Console.Write("请输入一个100内的整数:"); 2 string a = Console.ReadLine(); 3 int b = (int.Parse(a)) % 7; 4 string c = b == 0 ? "是" : "不是"; 5 int a1 = (int.Parse(a)); 6 string d = a1 >= 70 && a1 < 80 ? "是" : "不是"; 7 int a2 = (int.Parse(a)); 8 int b1 = (int.Parse(a)) % 10; 9 string e = b1 == 7 ? "是" : "不是"; 10 11 12 Console.WriteLine("“"+"是不是7的倍数:"+c+"”"); 13 Console.WriteLine("“"+"十位数是不是7:"+d+"”"); 14 Console.WriteLine("“"+"个位数是不是7:"+e+"”"); 15 16 17 18 Console.ReadLine(); 19 }
下面这个是上面第一个的简化版 也是思路最清晰的一种方法
1 Console.Write("请输入第一个数:"); 2 double a = Convert.ToDouble(Console.ReadLine()); 3 4 5 Console.Write("请输入第二个数:"); 6 double b = Convert.ToDouble(Console.ReadLine()); 7 8 9 Console.Write("请输入字符(+/-)"); 10 char c = Convert.ToChar(Console.ReadLine()); 11 char c1 = '+'; 12 13 14 double d = c == c1 ? a + b : a - b; 15 Console.WriteLine(d); 16 Console.ReadLine();
下面这个是上面第二个练习小题的另一种思路,核心思路不一样,但是套路相同
1 Console.Write("请输入一个0~100的整数:"); 2 string a1 = Console.ReadLine(); 3 int a = Convert.ToInt16(a1); 4 string b = a >= 50 && a != 66 ? "大" : "小"; 5 Console.Write(b); 6 7 8 9 Console.ReadLine();