1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace while4_求最大值 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //不断要求用户输入数字(假定用户输入的都是整数),当用户输入end的时候,输出最大值。 14 //循环体:提示用户输入一个数字,接收、转换成int类型,比较大小。 15 //循环条件:输入的不能是end 16 string input = ""; 17 int max = 0; 18 while (true) 19 { 20 Console.WriteLine("请输入一个数字,输入end时,输出最大值:"); 21 input = Console.ReadLine(); 22 try 23 { 24 if (input != "end") 25 { 26 int number = Convert.ToInt32(input); 27 if (number >= max) 28 { 29 max = number; 30 } 31 } 32 else 33 { 34 Console.WriteLine("您输入的最大值是{0}.", max); 35 break; 36 } 37 } 38 catch 39 { 40 Console.WriteLine("输入内容与要求不匹配,请重新输入:"); 41 } 42 } 43 Console.ReadKey(); 44 } 45 } 46 }