【问题标题】:Alternative for reading integer in C# [duplicate]在 C# 中读取整数的替代方法 [重复]
【发布时间】:2016-04-28 19:31:48
【问题描述】:

我知道使用Convert.To方法读取输入,但是除此之外还有什么方法可以读取。

int k = Convert.ToInt16(Console.ReadLine()); 

【问题讨论】:

  • 这不是你想要的吗?
  • 您实际上应该使用Convert.ToInt32。还有int.Parse
  • 是的,但我想要有没有其他方法
  • 好吧,如果你写一个大于 2^16 的数字(更精确的 2^16 - 1),前者最终会产生错误,而 Convert.ToInt32 会正常工作。
  • 这不是一个重复的问题吗?我敢肯定至少有 20 个类似的。

标签: c# input type-conversion inputstream beginread


【解决方案1】:

整数有 3 种类型:

1.) Short Integer:16 位数字(-32768 到 32767)。在 c# 中,您可以将短整型变量声明为 shortInt16

2.) "Normal" Integer:32 位数字(-2147483648 到 2147483647)。使用关键字intInt32 声明一个整数。

3.) Long Integer:64 位数字(-9223372036854775808 到 9223372036854775807)。用longInt64 声明一个长整数。

不同之处在于您可以使用的数字范围。 您可以使用Convert.ToParseTryParse 转换它们。

【讨论】:

    【解决方案2】:

    您可以为 Console 创建自己的实现并在任何您想要的地方使用它:

    public static class MyConsole
    {
        public static int ReadInt()
        {
            int k = 0;
            string val = Console.ReadLine();
            if (Int32.TryParse(val, out k))
                Console.WriteLine("You have typed a valid integer: " + k);
            else
                Console.WriteLine("This: " + val + " is not a valid integer");
            return k;
        }
    
        public static double ReadDouble()
        {
            double k = 0;
            string val = Console.ReadLine();
            if (Double.TryParse(val, out k))
                Console.WriteLine("You have typed a valid double: " + k);
            else
                Console.WriteLine("This: " + val + " is not a valid double");
            return k;
        }
        public static bool ReadBool()
        {
            bool k = false;
            string val = Console.ReadLine();
            if (Boolean.TryParse(val, out k))
                Console.WriteLine("You have typed a valid bool: " + k);
            else
                Console.WriteLine("This: " + val + " is not a valid bool");
            return k;
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            int s = MyConsole.ReadInt();
        }
    
    }
    

    【讨论】:

    • 这像用户定义的函数吗
    • 这些只是用户定义的函数)
    【解决方案3】:

    从控制台应用程序读取输入的最简单方法是Console.ReadLine。有可能的替代方案,但它们更复杂,并保留用于特殊情况:请参阅Console.ReadConsole.ReadKey

    然而,重要的是转换为不应使用Convert.ToInt32Int32.Parse 而是使用Int32.TryParse 完成的整数

    int k = 0;
    string input = Console.ReadLine();
    if(Int32.TryParse(input, out k))
        Console.WriteLine("You have typed a valid integer: " + k);
    else
        Console.WriteLine("This: " + input + " is not a valid integer");
    

    使用Int32.TryParse 的原因在于您可以检查是否可以转换为整数。相反,其他方法会引发异常,您应该处理使代码流程复杂化。

    【讨论】:

    • 您有编译时错误,因为k 变量未初始化。另外Console.WriteLine("This: " + k + " is not a valid integer"); 不显示错误的用户输入
    • 你当然是对的。没有直接思考,在我的辩护中,我可以说我把带有 k 值的 Console.WriteLine 作为事后的想法只是为了显示错误
    【解决方案4】:

    你可以使用int.TryParse

    查看示例

    var item = Console.ReadLine();
    int input;
    if (int.TryParse(item, out input))
    {
        // here you got item as int in input variable.
        // do your stuff.
        Console.WriteLine("OK");
    }
    else
        Console.WriteLine("Entered value is invalid");
    
    Console.ReadKey();
    

    【讨论】:

      【解决方案5】:

      这是您可以关注的另一种最佳method

      int k;
      if (int.TryParse(Console.ReadLine(), out k))
         {
            //Do your stuff here
         }
      else 
         {
            Console.WriteLine("Invalid input");
         }
      

      【讨论】:

        猜你喜欢
        • 2015-01-11
        • 1970-01-01
        • 1970-01-01
        • 2020-01-07
        • 1970-01-01
        • 2019-09-04
        • 2017-11-19
        • 2014-03-25
        • 1970-01-01
        相关资源
        最近更新 更多