当前位置:编程学习 > C#/ASP.NET >>

新手问一下 try与goto冲突这么解决.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
  
    class Program
    {
        #region Using delegate
        public delegate char PoXint(string str, int i);
        static char Poi(string cstr1, int i)
        {
            try
            {
                return cstr1[i];
            }
            catch {
                return cstr1[0];
            }
        }
        #endregion
        static void Main(string[] args)
        {
             PoXint Xint;
    loop2:        Console.WriteLine("请以xxxx/xxxx,的形式输入!");
            string input=Console.ReadLine();
            int i=input.IndexOf('/');
            if(i<3){
                Console.WriteLine("error the input !");
                goto loop2;
            }
            else{
                string s1=input.Substring(0,i);
                string s2=input.Substring(i+1,(input.Length-i-1));
               Xint=new PoXint(Poi);
                    //不需要delete,因为.NET有专门的垃圾收集器,每创建一个对象时就会纳入其监视对象中
               char c1=Xint(s1,3);
                char c2=Xint(s2,3);
                Console.WriteLine(c1+c2);
               Console.ReadLine();
            }

        }
    }
}


运行:
正常的。
请以xxxx/xxxx,的形式输入!
dsadsadsa/dasdes
200
不正常的:

请以xxxx/xxxx,的形式输入!
as/as
error the input !
请以xxxx/xxxx,的形式输入!


难道c#异常是用goto实现的! --------------------编程问答-------------------- 定一下不要沉! --------------------编程问答-------------------- 为什么听不懂呢,程序运行挺正常的哇 --------------------编程问答-------------------- --------------------编程问答-------------------- 这正常吗?我电脑是visual C# 2010学习板的。每次 catch异常都直接调用了loop2语句。 --------------------编程问答-------------------- 说的什么乱起八糟呀,你哪一次catch异常直接调用loop2了

你先看懂自己的程序好不好 --------------------编程问答-------------------- 你这程序进不去catch里面的…… --------------------编程问答-------------------- 如果要想进catch
  public delegate char PoXint(string str, int i);         
static char Poi(string cstr1, int i)         
{             
try            

char rsti= cstr1[i];                            
return rsti;
}             
catch {                 
return cstr1[0];             
}         

这样试试 --------------------编程问答-------------------- 另外
int i=input.IndexOf('/');             if(i<3){                 Console.WriteLine("error the input !");                 goto loop2;             } 这句不就让你永远避免错误参数的传递了吗? --------------------编程问答--------------------
引用 4 楼 u011751106 的回复:
这正常吗?我电脑是visual C# 2010学习板的。每次 catch异常都直接调用了loop2语句。

进入catch不会调用loop2的。

实际输入aaa/程序发生异常进入catch也没有再调用loop2啊。 --------------------编程问答--------------------  try
            {
                return cstr1[i];
            }
            catch {
                return cstr1[0];
            }

判断下是不是null会死啊

这也用的着catch。。。

补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,