【问题标题】:I am having trouble converting integer to string我在将整数转换为字符串时遇到问题
【发布时间】:2021-02-13 20:52:36
【问题描述】:
namespace C2360_Ch07_Console1_InRange
{
    class InRange
    {

        static void Main(string[] args)
        {
           

            Console.WriteLine("Num:");

            String theLine;
            theLine = Console.ReadLine();

            
            try
            {
                theLine = Convert.ToInt32(Console.ReadLine());//cant convert

            }

            catch (FormatException)
            {
                Console.WriteLine("Input string was not in a correct format.");
            }

            IsWithinRange(theLine);
        }

        static void IsWithinRange(String theLine)
        {
            int Line1;
            Int32.TryParse(Console.ReadLine(), out Line1);

            try
            {

                if ((Line1 < 1) || (Line1 > 10))
                {
                    throw new OverflowException();

                }

            }
            catch (OverflowException)
            {
                Console.WriteLine("number must be between 1 and 10.");
            }

            Console.ReadLine();
        }

    }

}

我想要做的是 convert stringinteger 以验证 MainIsWithinRange 方法。当我将theLine 转换为Int32.TryParse 时,我收到一个错误,评论所在的位置。这有可能吗? 有什么帮助吗?

【问题讨论】:

  • 你确定错误出现在 Int32.TryParse 而不是之前的 Convert.ToInt32?
  • 错误在 Convert.ToInt32 为什么?
  • 我猜你正在输入一个数字,然后再次按 Enter 键,因为没有任何反应:这是因为你为 theLine两次调用了Console.ReadLine() /跨度>
  • 很抱歉,她要我们串起来; theLine = Console.ReadLine();在主要。然后将 theLine 传递给 Main 中的 Convert.Int32。您需要使 IsWithinRange 具有 String 参数。当您从 Main 调用时,将 theLine 传递给该函数。然后你可以在 IsWithinRange 中调用 TryParse 时使用该字符串参数。
  • 首先将theLine 设置为字符串-String theLine = Console.ReadLine();。然后,您尝试通过再次调用 ReadLine 将其设置为整数 - `theLine = Convert.ToInt32(Console.ReadLine())'。

标签: c# integer


【解决方案1】:

我建议提取一个方法来读取整数值:

   private static int ReadInteger(string title) {
       while (true) {
           Console.WriteLine(title);

           if (int.TryParse(Console.ReadLine(), out int result))
               return result;

           Console.WriteLine("Incorrect syntax. Please, try again");
       }
   }    

然后使用它:

   static void Main(string[] args) {
       int theLine = 0;

       while (true) { 
         theLine = ReadInteger("Num:");

         // theLine is an integer, but we want an extra conditions meet:  
         if (theLine >= 1 && theLine <= 10)
           break;

         Console.WriteLine("The value must be in [1..10] range. Please, try again"); 
       }

       // from now on theLine is an integer in [1..10] range 
       //TODO: put relevant code here
   }

请注意,异常 FormatExceptionOverflowException 用于异常行为;这里(用户输入验证),好老的if 就够了。

编辑:如果你不想提取方法(为什么?)但保留IsWithinRange,你可以这样写:

   static void Main(string[] args) {
       int theLine = 0;

       while (true) {
           Console.WriteLine("Num:");

           if (!int.TryParse(Console.ReadLine(), out theLine)) {
               Console.WriteLine("Syntax error. Please, try again");

               continue;
           } 

           if (IsWithinRange(theLine))
               break;

           Console.WriteLine("Sorry, the value is out of range. Please, try again"); 
       }
       // from now on theLine is an integer in [1..10] range 
       //TODO: put relevant code here

IsWithinRange 可以在哪里

   // Least Confusion Principle: For "Is [the value] Within Range" question
   // the expected answer is either true or false
   private static bool IsWithinRange(int value) {
       return value >= 1 && value <= 10;
   }

【讨论】:

  • Dmitry Bychenko,这不是她想要的方式。她只想要主要的并且在范围内。
猜你喜欢
  • 1970-01-01
  • 2020-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-25
  • 2012-09-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多