编程实现在一行内输入若干个整数(不超过30个),输出最大的一个整数。
样例如下:
输入(在一行内输入,空格分隔):
5 6 78 -89 0 23 100 4 6
输出:
100
C#具体设计:
1.先让用户输入一些整数,
2.再装其读入到字符串用,
3.用空格符来分割字符成,存成字符数组
4.用foreach语句进行数组遍历,将字符转成整型后,比较大小,找出最大整数
5.输出最大整数
完整C#代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { System.Console.Write("Please Input Number..."); string strnums = Console.ReadLine(); string[] arr = strnums.Split(new char[] { ' ' }); int max = int.MinValue; foreach(string str in arr) { int temp = Convert.ToInt32(str); if (max < temp) max = temp; } Console.WriteLine(max); Console.Read(); } } } |