namespaceConsoleApp3 { classProgram { staticvoidMain(string[] args) { //设鸡有i只,兔有j只 //i+j=h;2i+4j=f; int i, j, h, f;
Console.Write("请输入鸡和兔子共有多少只: "); String s = Console.ReadLine(); h = int.Parse(s); Console.Write("请输入共有多少只脚(必须是偶数): "); s = Console.ReadLine(); f = int.Parse(s);
while(f%2!=0) { Console.WriteLine("脚的个数必须是偶数!请重新输入!"); Console.Write("请输入共有多少只脚(必须是偶数): "); s = Console.ReadLine(); f = int.Parse(s); }
namespaceConsoleApp3 { classProgram { staticvoidMain(string[] args) { //设鸡有i只,兔有j只 //i+j=h;2i+4j=f; int i, j, h, f;
Console.Write("请输入鸡和兔子共有多少只: "); String s = Console.ReadLine(); h = int.Parse(s); Console.Write("请输入共有多少只脚(必须是偶数): "); s = Console.ReadLine(); f = int.Parse(s);
while(f%2!=0) { Console.WriteLine("脚的个数必须是偶数!请重新输入!"); Console.Write("请输入共有多少只脚(必须是偶数): "); s = Console.ReadLine(); f = int.Parse(s); }
bool solution = false;
j = f / 2 - h; i = h - j;
if(i>=0&&j>=0) { Console.WriteLine("鸡有{0},兔有{1}", i, j); solution = true; }
namespaceConsoleApp3 { classProgram { staticvoidMain(string[] args) { double pi = 0; double t = 1; double n = 1; int s = 1; while(Math.Abs(t)>=Math.Pow(10,-6))//精度 { pi += 4*t; n += 2; s = -s; t = s / n; } Console.WriteLine("pi={0}", pi); Console.ReadKey(); } } }
namespaceConsoleApp3 { classProgram { staticvoidMain(string[] args) { int m, n, m1, n1; int i;
//获得用户输入的两个数 Console.Write("请输入第一个整数: "); String s = Console.ReadLine(); m = int.Parse(s); Console.Write("请输入第二个整数: "); s = Console.ReadLine(); n = int.Parse(s);
/*生成两个范围在(0-100)的随机数的代码 Random rNum = new Random(); m = rNum.Next(0, 101); n = rNum.Next(0, 101); */
//比较两个数的大小 if (m<n) { m1 = m; n1 = n; } else { m1 = n; n1 = m; }
namespaceConsoleApp3 { classProgram { staticvoidMain(string[] args) { int i, j, k; Console.Write("请输入被除数: "); String s = Console.ReadLine(); i = int.Parse(s); Console.Write("请输入除数: "); s = Console.ReadLine(); j = int.Parse(s);
k = i / j;
Console.WriteLine("{0}/{1}={2}", i, j, k);
Console.ReadKey(); } } }
1 2 3 4
请输入被除数: 6 请输入除数: 0 Unhandled exception. System.DivideByZeroException: Attempted to divide by zero. at ConsoleApp3.Program.Main(String[] args) in J:\code\C#\ConsoleApp3\Program.cs:line 17
1 2 3 4 5 6 7
请输入被除数: 6 请输入除数: a Unhandled exception. System.FormatException: Input string was notin a correct format. at System.Number.ThrowOverflowOrFormatException(ParsingStatus status, TypeCode type) at System.Number.ParseInt32(ReadOnlySpan`1value, NumberStyles styles, NumberFormatInfo info) at System.Int32.Parse(String s) at ConsoleApp3.Program.Main(String[] args) in J:\code\C#\ConsoleApp3\Program.cs:line 15