回 帖 发 新 帖 刷新版面

主题:异常处理

/*关于异常处理的问题,详见下面叙述*/


using System;

namespace ex71
{
    class Class1
    {
        static void Main(string[] args)
        {
            int[] intNumber=new int[5];
            for(int i=0;i<intNumber.Length;i++)
            {
                try
                {
                    Console.WriteLine("请输入第{0}个元素:",i+1);
                    intNumber[i]=Convert.ToInt16(Console.ReadLine());
                }
                
                //引发此异常时
                catch(System.FormatException)
                {
                    //输出此语句,并让它返回当前元素,那么这句话该怎么写,本人是新手,请高手赐教
                    Console.WriteLine("输入的应为数字,请重新输入。");
                }
            }

            Console.WriteLine("\n");

            for(int i=0;i<intNumber.Length;i++)
            {
                
                Console.WriteLine("{0}",intNumber[i]);
            }
            

回复列表 (共2个回复)

沙发

请高手回复此帖

板凳

using System;

namespace ex71
{
    class Class1
    {
        static void Main(string[] args)
        {
            int[] intNumber=new int[5];
            for(int i=0;i<intNumber.Length;i++)
            {
                try
                {
                    Console.WriteLine("请输入第{0}个元素:",i+1);
                    intNumber[i]=Convert.ToInt16(Console.ReadLine());
                }
                
                    //引发此异常时
                catch(System.FormatException)
                {
                    //输出此语句,并让它返回当前元素,那么这句话该怎么写,本人是新手,请高手赐教
                    
                    i--;   //就是这句

                    Console.WriteLine("输入的应为数字,请重新输入。");
                }
            }

            Console.WriteLine("\n");

            for(int i=0;i<intNumber.Length;i++)
            {
                
                Console.WriteLine("{0}",intNumber[i]);
            }
        }
    }
}

我来回复

您尚未登录,请登录后再回复。点此登录或注册