【问题标题】:Adjusting the console apliacation (trycatch and while loop)调整控制台应用程序(trycatch 和 while 循环)
【发布时间】:2017-03-22 02:02:08
【问题描述】:

所以在这段代码中,我试图让它在用户输入超出范围的值时显示消息:

输入一个有效的输入(1 到 2^53 之间)

此时它正在做的是,当你输入一个字母时会出现消息,但是当你输入一个小于 0 的数字时,它只是重置循环并继续,就好像什么都没发生一样。

//variables
double length, width, totalarea, totallength;
const double feet = 3.75;

//questions
Console.Title = "Double Glazing Window Calculator";
Console.WriteLine("Double Glazing Calculator\n");
bool InputFalse = false;

do
{
   try
   {
      do
      {
          Console.Write("Enter the height of the of the window in meteres ");           
          length = double.Parse(Console.ReadLine());
          Console.Write("Enter the width of the of the window in meteres ");
          width = double.Parse(Console.ReadLine());   
      } while (length < 1 || width < 1);

      //maths
      totalarea = length * width * 2;
      totallength = (length * 2 + width * 2) * feet;
      Console.WriteLine("The total area of the glass required in m^2 (to 2 decinmal places) is {0} ", totalarea.ToString("0.##"));

      Console.WriteLine("the total length of the wood required in feet (to 2 decimal places) is {0}", totallength.ToString("0.##"));             
   }
   catch
   {
      InputFalse = (true);
      Console.WriteLine("Enter a valid input (between 1 and 2^53)");
   }
} while (true);

【问题讨论】:

    标签: c# while-loop logic try-catch console-application


    【解决方案1】:

    与其从double.Parse 捕获异常,不如使用double.TryParse。由于您只需要至少为 1 的值,因此您可以使用 double.TryParse 在解析失败时将 out 参数设置为 0 的事实。

    double length = 0, width = 0;
    const double feet = 3.75;
    
    //questions
    Console.Title = "Double Glazing Window Calculator";
    Console.WriteLine("Double Glazing Calculator\n");
    
    while (true)
    {
        Console.Write("Enter the height of the of the window in meteres ");
        double.TryParse(Console.ReadLine(), out length);
        Console.Write("Enter the width of the of the window in meteres ");
        double.TryParse(Console.ReadLine(), out width);
    
        if (length < 1 || width < 1)
        {
            Console.WriteLine("Enter a valid input (between 1 and 2^53)");
        }
        else
        {
            break;
        }
    }
    
    //maths
    var totalarea = length * width * 2;
    var totallength = (length * 2 + width * 2) * feet;
    Console.WriteLine("The total area of the glass required in m^2 (to 2 decinmal places) is {0} ", totalarea.ToString("0.##"));
    
    Console.WriteLine("the total length of the wood required in feet (to 2 decimal places) is {0}", totallength.ToString("0.##"));
    

    您甚至可以将其拆分为 2 个while 循环,这样在它们首先输入有效长度之前,它不会让它们输入宽度。

    【讨论】:

      【解决方案2】:

      我建议使用一种输入方法,这将使代码更具可读性,就像@juharr 建议使用TryParse 以避免需要try-catch 块。

      例子:

      double length = 0, 
             width = 0, 
             totalarea, 
             totallength;
      const double feet = 3.75;
      
      //questions
      Console.Title = "Double Glazing Window Calculator";
      Console.WriteLine("Double Glazing Calculator\n");
      bool InputFalse = false;
      
      while(true){
          GetAndVerifyInput(out length, "Enter the length of the of the window in meteres: ");
          GetAndVerifyInput(out width, "Enter the width of the of the window in meteres: ");
      
          //maths
          totalarea = length * width * 2;
          totallength = (length * 2 + width * 2) * feet;
          Console.WriteLine();
          Console.WriteLine("The total area of the glass required in m^2 (to 2 decimal places) is {0} ", totalarea.ToString("0.##"));
          Console.WriteLine("the total length of the wood required in feet (to 2 decimal places) is {0}", totallength.ToString("0.##"));
          Console.WriteLine();
          Console.WriteLine();
      }
      
      
      private void GetAndVerifyInput(out double result, string instruction) {
          while (true)
          {
              Console.Write(instruction);
              if (double.TryParse(Console.ReadLine(), out result) && 
                  result > 0 && 
                  result < Math.Pow(2,53) + 1)
                  return;
              Console.WriteLine("Enter a valid input (between 1 and 2^53)");
           }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-03-14
        • 1970-01-01
        • 1970-01-01
        • 2016-05-01
        • 2017-06-07
        • 1970-01-01
        • 2016-06-30
        相关资源
        最近更新 更多