异常捕获
-
try--catch
double num = 0;
int fg = 1;
Console.WriteLine("请输入一个数字");
try
{
num = Convert.ToDouble(Console.ReadLine());
}
catch {
Console.WriteLine("输入内容有误");
fg = 0;
}
if (fg == 1) {
Console.WriteLine(2*num);
}
Console.ReadKey();
三元表达式
int n1 = Convert.ToInt32 (Console.ReadLine());
int n2 = Convert.ToInt32(Console.ReadLine());
int Max = n1 > n2 ? n1 : n2;
Console.WriteLine(Max);
Console.ReadKey();
随机数
//产生1-10的随机数
Random r = new Random();
int num = r.Next(1, 11);
Console.WriteLine(num);
Console.ReadKey();