当前位置: 代码迷 >> C# >> 初学C#遇到有关问题,求高手解答
  详细解决方案

初学C#遇到有关问题,求高手解答

热度:315   发布时间:2016-05-05 05:20:48.0
初学C#,遇到问题,求高手解答

当输入字母等非数字时,发生未经处理的异常,请问如何解决
比如怎样才能输入字母时,输出"输入错误!”
代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static double San(double a, double b)
        {
            return a*b*0.5;
        }
        static double JuAndPing(double a, double b)
        {
            return a * b;
        }
        static double Yuan(double a)
        {
            return a * a * Math.PI;
        }
        static double Shan(double a, double b)
        {
            return a * b / 2;
        }
        
        static void Main(string[] args)
        {
            double x, y;
            Console.WriteLine("****************");
            Console.WriteLine(" 图形面积计算器 ");
            Console.WriteLine("****************");
            Console.WriteLine("1.三角形");
            Console.WriteLine("2.矩形");
            Console.WriteLine("3.圆形");
            Console.WriteLine("4.扇形");
            Console.WriteLine("5.平行四边形");
            Console.WriteLine("请输入你的选择:");
            int choice = Int32.Parse(Console.ReadLine());
            switch (choice)
            {
                case 1:
                    Console.Write("请输入高:");
                    x = double.Parse(Console.ReadLine());
                    Console.Write("请输入边:");
                    y = double.Parse(Console.ReadLine());
                    Console.Write("三角形面积为:");
                    Console.WriteLine(San(x, y));
                    break;
                case 2:
                    Console.Write("请输入长:");
                    x = double.Parse(Console.ReadLine()); 
                    Console.Write("请输入高:");
                    y = double.Parse(Console.ReadLine());
                    Console.Write("矩形面积为:");
                    Console.WriteLine(JuAndPing(x, y));
                    break;
                case 3:
                    Console.Write("请输入半径:");
                    x = double.Parse(Console.ReadLine());
                    Console.Write("圆的面积为:");
                    Console.WriteLine(Yuan(x));
                    break;
                case 4:
                    Console.Write("请输入半径:");
                    x = double.Parse(Console.ReadLine());
                    Console.Write("请输入弧长:");
                    y = double.Parse(Console.ReadLine());
                    Console.Write("扇形面积为:");
                    Console.WriteLine(Shan(x, y));
                    break;
                case 5:
                    Console.Write("请输入底边:");
                    x = double.Parse(Console.ReadLine());
                    Console.Write("请输入高:");
                    y = double.Parse(Console.ReadLine());
                    Console.Write("平行四边形面积为:");
                    Console.WriteLine(JuAndPing(x, y));
                    break;
                default: Console.WriteLine("输入错误,请输入1~5");
                    break;
            }
        }
    }
}
------解决思路----------------------
bool isNum = false;
 double x = 0.0;
Console.Write("请输入高:");
 while (!isNum)
 {
     isNum = double.TryParse(Console.ReadLine(), out x);
     if (isNum) break;
     Console.Write("输入不正确,请再次输入高:");
 } 
  相关解决方案