1.将百分制转换成五分制,如果输入的百分制成绩超出0~100时,程序抛出异常。
代码如下:
using System;
using System.Collections;
using System.Collections.Generic;
namespace Program
{
class OverflowRange : ApplicationException
{
public OverflowRange(string msg):base(msg)
{ }
}
class Program
{
static void Main(string[] args)
{
try
{
Console.Write("输入一个百分数:");
int num = Convert.ToInt32(Console.ReadLine());
if (num < 0 || num > 100)
{
throw new OverflowRange("数值不在0~100之间");
}
double result = (double)num * 5.0 / 100;
Console.WriteLine("转换成五分制为:{0}", result);
}
catch (FormatException)
{
Console.WriteLine("必须输入数字");
}