1、用 if 判断输入的是否是空格键的方法:(Console.ReadKey()的用法)
ConsoleKeyInfo readkey = Console.ReadKey(); Console.WriteLine(readkey.Key.ToString()); if (readkey.ToString().ToLower() == "spacebar") Console.WriteLine("空格");
2、用 if……else 判断是否为闰年(闰年为能被4整除的年份,*注:若该年份能被100整除,还要被400整除才是闰年)
Console.WriteLine("请输入年份:"); int year = int.Parse(Console.ReadLine()); if (year % 4 == 0) { if (year % 100 == 0) { if (year % 400 == 0) { Console.WriteLine("yes"); } else Console.WriteLine("no"); } else Console.WriteLine("yes"); } else Console.WriteLine("no");
3、if……else if……else 输入一个1~100之内的数,判断是否与7有关
Console.WriteLine("请输入一个1~100内的数"); int shu = int.Parse(Console.ReadLine()); if (shu >= 1 && shu <= 100) { if (shu%10==7) Console.WriteLine("yes"); else if(shu%7==0) Console.WriteLine("yes"); else if(shu/10==7) Console.WriteLine("yes"); else Console.WriteLine("no"); } else { Console.WriteLine("输入错误"); }
4、输入a,b,c,求一元二次方程的解
Console.WriteLine("请输入a,b,c"); int a = int.Parse(Console.ReadLine()); int b = int.Parse(Console.ReadLine()); int c = int.Parse(Console.ReadLine()); if (a == 0 && b != 0) { double x =(double) (-c) / b; Console.WriteLine("有一个实根{0}",x); } else if (a == 0 && b == 0) { Console.WriteLine("a,b均为零,无实根"); } else { double de =sqr( b * b - 4 * a * c); if (de == 0) { double x = (double)(-b) / (2 * a); Console.WriteLine("有两个相等的实根,x1=x2={0}", x); } else if (de < 0) { Console.WriteLine("此方程无解"); } else { double x1 = (double)((-b) + Math.Sqrt(de)) / (2 * a); double x2 = (double)((-b) - Math.Sqrt(de)) / (2 * a); Console.WriteLine("有两个实根,x1={0},x2={1}",x1,x2); } }