写一个程序,找出输入的一连串整数的最大最小值。程序首先应提醒用户指定要输入多少个值。接着打印这个值,并要求用户确认。如果用户没有对这个值进行确认,就必须输入一个新的值。
如何编写上述程序
C#语言.

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.Text;namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                int min = int.MaxValue, max = int.MaxValue*-1;
                int i = 0;
                Console.WriteLine("请输入一个整数并回车确认,退出请直接回车!");
                string s = Console.ReadLine();
                while (s != "") 
                {
                    int n = IsInt(s);
                    if (min > n)
                        min = n;
                    if (max < n)
                        max = n;                i++;
                    Console.WriteLine("您已经输入" + i.ToString() + "个数,最小为" + min.ToString() + ",最大为" + max.ToString() + ",请输入一个整数并回车确认,退出请直接回车!");
                    s = Console.ReadLine();
                }
            }        public static int IsInt(string s)
            {
                if(s==null || s.Length==0)
                    return 0;
                try
                {
                    return int.Parse(s);
                }
                catch
                {
                    return 0;
                }
            }
        }
    }
      

  2.   

    using System;
    using System.Collections.Generic;
    using System.Text;namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                int min = int.MaxValue, max = int.MaxValue*-1;
                int i = 0;
                Console.WriteLine("请输入一组整数中间用逗号隔开,并回车确认,退出请直接回车!");
                string s = Console.ReadLine();
                while (s != "") 
                {
                    foreach (string st in s.Split(','))
                    {
                        int n = IsInt(st);
                        if (min > n)
                            min = n;
                        if (max < n)
                            max = n;
                    }
                    i++;
                    Console.WriteLine("您已经输入" + i.ToString() + "组数,最小为" + min.ToString() + ",最大为" + max.ToString() + ",请输入一组整数中间用逗号隔开,退出请直接回车!");
                    s = Console.ReadLine();
                }
            }        public static int IsInt(string s)
            {
                if(s==null || s.Length==0)
                    return 0;
                try
                {
                    return int.Parse(s);
                }
                catch
                {
                    return 0;
                }
            }
        }
    }
      

  3.   

    int min = int.MaxValue, max = int.MaxValue*-1;
    能直接这样写代码么?