2016-11-08 47 views
0

因此,在這個代碼我試圖讓這個如果用戶輸入一個值超出範圍就會顯示消息:調整控制檯apliacation(trycatch和while循環)

輸入一個有效的輸入(在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); 

回答

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循環,因此不會讓他們進入的寬度,直到他們第一次進入有效長度。

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)"); 
    } 
}