【发布时间】: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