问题描述:
输入10个整数放入数组,求最大值,最小值,平均值。
代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int max = 0, min = 10, sum = 0, i = 0, n,aver; int[] a = new int[10]; string s; Console.WriteLine("请输入十个整数:\n"); while (i < a.Length) { s = Console.ReadLine(); int.TryParse(s, out a[i]); sum = sum + a[i]; i++; } max = a.Max(); min = a.Min(); aver = sum / 10; Console.WriteLine("这十个数中最大值是{0},最小值是{1},平均值是{2}", max, min, aver); Console.ReadKey(); } } }运行结果:
问题描述:
输入若干个整数放入数组,求最大值,最小值,平均值。
代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int max = 0, min = 10, sum = 0, i = 0, n,aver; int[] a = new int[1000]; string s; Console.WriteLine("请输入整数:\n"); while (true) { s = Console.ReadLine(); int.TryParse(s, out a[i]); if (s == "over") break; i++; } n = i; for (i = 0; i < n; i++) { if (a[i] > max) max = a[i]; if (a[i] < min) min = a[i]; sum = sum + a[i]; } aver = sum / n; Console.WriteLine("这些整数中最大值是{0},最小值是{1},平均值是{2}", max, min, aver); Console.ReadKey(); } } }运行结果:
随机给0-99的数字,猜数字进行比较。
代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Random n = new Random(); int i = n.Next(0, 99); Console.WriteLine("系统随机给出了一个数,请输入你猜的数值与随机数进行比较:\n"); Console.WriteLine("你猜的数是:"); int s; while (true) { s = int.Parse(Console.ReadLine()); if (s > i) { Console.WriteLine("猜大了..."); } else if (s < i) { Console.WriteLine("猜小了..."); } else if (s == i) { break; } } Console.WriteLine("猜对啦..."); Console.ReadKey(); } } }运行结果: