将百分制转换为五分制,如果输入的成绩超过了0~100时,抛出异常
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class OverflowRange : ApplicationException
{
public OverflowRange(string msg)
: base(msg)
{ }
}
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("请您输入百分制分数:");
double x = double.Parse(Console.ReadLine());
double s;
if (x < 0||x>100)
{
throw new OverflowRange("不允许输入超过0~100的数!");
}
s = x * 0.05;
Console.WriteLine("{0}对应的五分制分数是:{1}", x, s);
// Console.ReadKey();
}
catch (FormatException)
{
Console.WriteLine("必须输入数字!");
}
catch (OverflowRange e)
{
Console.WriteLine(e.Message);
}
finally
{
Console.WriteLine("请按任意键退出......");
Console.ReadLine();
}
}
}
}