哪位高手帮忙看下下列代码!!!
有异常!!帮忙看下感谢!!!
using System;
using System.Collections.Generic;
using System.Text;namespace card
{
    class Card
    {
        private string name;
        private string auther;
        private int count = 0;
        public void store()
        {
            Console.WriteLine("请输入要添加的书名:");
            name = Console.ReadLine();
            Console.WriteLine("请输入所添加书的作者:");
            auther = Console.ReadLine();
            Console.WriteLine("请输入要添加书的数量:");
            count += Convert.ToInt32(Console.ReadLine());
        }
        public void show()
        {
            Console.WriteLine("{0,-20}{1,-20}{2,-20}", name, auther,count);
        }
        public void swap(ref string a,ref string b)
        {
            string temp;
            temp = a;
            a = b;
            b = temp;
        }
        public void sort(Card[] a, int num)
        {
            int i, j;
            int choice;
            choice = Convert.ToInt32(Console.ReadLine());
            if (choice == 1)
            {
                for (i = 0; i < a.Length - 1; i++)
                    for (j = a.Length - 1; j > i; j--)
                        if (string.Compare(a[i].name, a[j].name) > 0)
                        {
                            swap(ref a[i].name, ref a[j].name);
                            swap(ref a[i].auther, ref a[j].auther);
                        }
                Console.WriteLine("书名                作者              馆藏数量");
                for (i = 0; i < num; i++)
                    a[i].show();
                Console.Read();
            }
            if (choice == 2)
            {
                for (i = 0; i < a.Length - 1; i++)
                    for (j = a.Length - 1; j > i;j-- )
                        if (string.Compare(a[i].auther, a[j].auther) > 0)
                        {
                            swap(ref a[i].auther, ref a[j].auther);
                            swap(ref a[i].name, ref a[j].name);
                        }
                Console.WriteLine("作者                书名              馆藏数量");
                for (i = 0; i < num; i++)
                    a[i].show();
                Console.Read();
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            int i, num;
            Console.WriteLine("请输入要入库的数量:");
            num = Convert.ToInt32(Console.ReadLine());
            Card[] book = new Card[num];
            for (i = 0; i < num; i++)
            {
                book[i] = new Card();
                book[i].store();
            }
            Console.WriteLine("请选择你要排序的方法:");
            Console.WriteLine("1-按书名排序");
            Console.WriteLine("2-按作者排序");
            for (i = 0; i < num; i++)
            {
                book[i].sort(book, num);
            }        }
    }
}

解决方案 »

  1.   

    未处理 System.FormatException
      Message="输入字符串的格式不正确。"
      Source="mscorlib"
      StackTrace:
           在 System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
           在 System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
           在 System.Convert.ToInt32(String value)
           在 card.Card.sort(Card[] a, Int32 num) 位置 F:\Joyce的东东\Joyce的C#程序\实验一(1)\实验一(1)\Program.cs:行号 36
           在 card.Program.Main(String[] args) 位置 F:\Joyce的东东\Joyce的C#程序\实验一(1)\实验一(1)\Program.cs:行号 85
           在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
           在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           在 System.Threading.ThreadHelper.ThreadStart()
      InnerException: 
      

  2.   

    你的异常是在排序的时候输入了非数字字符。加入try就不会报错了              int choice=0;
                try
                {
                    choice = Convert.ToInt32(Console.ReadLine());
                }
                catch { ;}
      

  3.   

    public bool IsNumber(String strNumber)
    {
    Regex objNotNumberPattern=new Regex("[^0-9.-]");
    Regex objTwoDotPattern=new Regex("[0-9]*[.][0-9]*[.][0-9]*");
    Regex objTwoMinusPattern=new Regex("[0-9]*[-][0-9]*[-][0-9]*");
    String strValidRealPattern="^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
    String strValidIntegerPattern="^([-]|[0-9])[0-9]*$";
    Regex objNumberPattern =new Regex("(" + strValidRealPattern +")|(" + strValidIntegerPattern + ")");return !objNotNumberPattern.IsMatch(strNumber) &&
    !objTwoDotPattern.IsMatch(strNumber) &&
    !objTwoMinusPattern.IsMatch(strNumber) &&
    objNumberPattern.IsMatch(strNumber);
    }用这个函数就能实现,如果不是数字,返回false