使用try catch判断是否输入为数字

来源:互联网 发布:高学历 女友 知乎 编辑:程序博客网 时间:2024/06/06 12:53
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Try_Catch_q{    class Program    {        static void Main(string[] args)        {            int input;            bool flag = true;            do            {                //使用try catch来进行判断输入的是否是数字                try                {                    Console.WriteLine("请输入一个数字");                    input = Convert.ToInt32(Console.ReadLine());                    Console.WriteLine("{0}的二倍是{1}", input, input * 2);                    flag = true;                }                catch                {                    flag = false;                }            } while (flag);            Console.ReadKey();        }    }}

0 0
原创粉丝点击