int A = (System.Console.ReadLine("输入文科均分:"));
int B = (System.Console.ReadLine("输入理科均分:"));我想判断是否为整型,或者干脆禁止输入整型以外的值。该怎么写?

解决方案 »

  1.   


    //正则匹配
    string str = Console.ReadLine();
                    string pattern = "^[0-9]*[1-9][0-9]*$";
                    Regex reg = new Regex(pattern);
                    while (!reg.IsMatch(str))
                    {
                        Console.WriteLine("非法输入,请重新输入");
                        str = Console.ReadLine();
                    }
      

  2.   

    static void Main(string[] args)
            {
                int num = 0;
                bool result = false;
                while (!result)
                {
                    Console.WriteLine("input:");
                    string input = Console.ReadLine();
                    result = int.TryParse(input, out num);
                }            Console.WriteLine("success");            Console.ReadKey();
            }
      

  3.   

    暴力点
    try
    {
          string input = Console.ReadLine();
          Convert.ToInt32(input);
     Console.WriteLine(input);
    }
    catch
    {
           Console.WriteLine("请输入与整形");
    }可以判断任何类型。 
      

  4.   

    我觉得也应当用异常处理
    但我的变量是A与B啊
    不需要转换,而是返回。C#的异常处理我还没学到
      

  5.   

    异常处理不改变我那两行代码用return返回行吗?
      

  6.   

    上面月神的处理方式就是了。你要输入两个数,把他的while循环再复制下就是了。就是里面的int改成uint,不然负整数也会被承认。
      

  7.   

            Dim indata As String = "123456"
            Try
                If indata = CInt(indata).ToString Then
                    '///  是整型
                Else
                    '/// 不是整型
                End If
            Catch ex As Exception
                '///  连数都不是.
            End Try
      

  8.   

    用VB的函数 IsNumeric  也可以.比用 TRY 要好.        Dim indata As String = "123456"
            If IsNumeric(indata) AndAlso indata = CInt(indata).ToString Then
                '///  是整型
                Me.Text = indata
            Else
                '/// 不是整数
            End If
      

  9.   

         string m = "aa";
                int b = 0;
                bool rlt = int.TryParse(m, out b);
      

  10.   

    int.parse(console.readline());
    这也是一种转换方式。专用于字符串转换为值类型 。