关于异常抛出的问题

KingZ_c1ear 2009-04-06 10:03:06
namespace kehou1
{
class Program
{
static void Main(string[] args)
{
lbstart:
try
{
System.Console.Write("请输入字母:");
int cTemp = System.Console.Read();
if (cTemp >= 'a' && cTemp <= 'z')
ShowToUpper(cTemp);
else if (cTemp >= 'A' && cTemp <= 'Z')
ShowToLower(cTemp);
else
{
throw new Exception();
}
}
catch (Exception ex1)
{
System.Console.WriteLine("非字母!{0}", ex1.Message);
}
goto lbstart;
}
public static void ShowToUpper(int cTemp)
{
char sTemp = (char)(cTemp - 32);
System.Console.WriteLine("{0}", sTemp);
}
public static void ShowToLower(int cTemp)
{
char sTemp = (char)(cTemp + 32);
System.Console.WriteLine("{0}", sTemp);
}
}
}



这是我写的代码 要的是在输入非字母是 抛出异常提示为字母重新输入, 但是我怎么要执行几次呢?
...全文
98 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Garnett_KG 2009-04-07
  • 打赏
  • 举报
回复
把 cTemp = System.Console.Read(); 改成cTemp = System.Console.ReadLine()[0];


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

namespace kehou1
{
class Program
{
static void Main(string[] args)
{
int cTemp;
lbstart:
try
{
System.Console.Write("請輸入字母:");
cTemp = System.Console.ReadLine()[0];
if (cTemp >= 'a' && cTemp <= 'z')
ShowToUpper(cTemp);
else if (cTemp >= 'A' && cTemp <= 'Z')
ShowToLower(cTemp);
else
{
throw new MyException("異常情況!");
}
}
catch (Exception ex1)
{
System.Console.WriteLine("{0}", ex1.Message);

goto lbstart;
}

}
public static void ShowToUpper(int cTemp)
{
char sTemp = (char)(cTemp - 32);
System.Console.WriteLine("{0}", sTemp);
}
public static void ShowToLower(int cTemp)
{
char sTemp = (char)(cTemp + 32);
System.Console.WriteLine("{0}", sTemp);
}
public class MyException : ApplicationException
{
public MyException(string strMsg)
: base(strMsg)
{
}
}
}
}

烈火蜓蜻 2009-04-07
  • 打赏
  • 举报
回复

static void Main(string[] args)
{
int cTemp;
while(true)
{
try
{
System.Console.Write("请输入字母:");
cTemp = System.Console.Read();
if (cTemp >= 'a' && cTemp <= 'z')
ShowToUpper(cTemp);
else if (cTemp >= 'A' && cTemp <= 'Z')
ShowToLower(cTemp);
else
{
throw new MyException("异常情况!");
}
break;
}
catch (Exception ex1)
{
System.Console.WriteLine("{0}",ex1.Message);
}
}
}

KingZ_c1ear 2009-04-07
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Text;

namespace kehou1
{
class Program
{
static void Main(string[] args)
{
int cTemp;
lbstart:
try
{
System.Console.Write("请输入字母:");
cTemp = System.Console.Read();
if (cTemp >= 'a' && cTemp <= 'z')
ShowToUpper(cTemp);
else if (cTemp >= 'A' && cTemp <= 'Z')
ShowToLower(cTemp);
else
{
throw new MyException("异常情况!");
}
}
catch (Exception ex1)
{
System.Console.WriteLine("{0}",ex1.Message);
goto lbstart;
}

}
public static void ShowToUpper(int cTemp)
{
char sTemp = (char)(cTemp - 32);
System.Console.WriteLine("{0}", sTemp);
}
public static void ShowToLower(int cTemp)
{
char sTemp = (char)(cTemp + 32);
System.Console.WriteLine("{0}", sTemp);
}
public class MyException :ApplicationException
{
public MyException (string strMsg):base (strMsg)
{
}
}
}
}
现在我可以正常抛出异常了 但是如果输入不正确的话 会把TRY以下的语句执行两次 以后在输入 怎么回事呢? WHILE 怎么用?
jie3614 2009-04-07
  • 打赏
  • 举报
回复
用While语句
KingZ_c1ear 2009-04-07
  • 打赏
  • 举报
回复
哦! 我也不想这样 书上要求这样写 。。。。 我试试
yangqidong 2009-04-06
  • 打赏
  • 举报
回复
完全可以不用异常。。。
不过如果你还是要用,把 goto lbstart;放入catch里面
catch (Exception ex1)
{
System.Console.WriteLine("非字母!{0}", ex1.Message);
goto lbstart;
}
默默_XL 2009-04-06
  • 打赏
  • 举报
回复
不太懂lz的意思哎···
可以自定义个异常
如果要反复使用的话 就要自己定义异常 按自己的需要定义
想在哪个函数用 调咯就行

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧