调整控制台apliacation(trycatch和while循环)

时间:2016-11-08 12:29:11

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

所以在这段代码中我试图让它成为如果用户输入一个超出范围的值,它将显示消息:

  

输入有效输入(介于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);

2 个答案:

答案 0 :(得分: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循环,这样它们就不会让它们输入宽度,直到它们首先输入有效长度。

答案 1 :(得分:0)

我建议使用输入方法,这会使代码更具可读性,并且@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)");
     }
}