1、用 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");
2、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("輸入錯誤"); }
3、輸入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); } }