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实现的!
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实现的!
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; } 这句不就让你永远避免错误参数的传递了吗?
{
return cstr1[i];
}
catch {
return cstr1[0];
}判断下是不是null会死啊这也用的着catch