2016-04-29 60 views
2

所以基本上我編寫了一個計算三角形面積的小程序。但是,即使輸入正數,我也會收到數字爲負數的警告。它在我添加負數檢查之前起作用。任何人都可以幫助我?如果數字是負數,我希望程序關閉,但即使數字是正數,它也會關閉

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace ConsoleApplication5 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
     double num; 
     Console.WriteLine("Base length: "); 
     num = Convert.ToDouble(Console.ReadLine()); 
     if (num < 0); 
     { 
      Console.WriteLine("The number cannot be negative"); 
      Console.ReadKey(); 
      return; 
     } 
     double num2; 
     Console.WriteLine("Height: "); 
     num2 = Convert.ToDouble(Console.ReadLine()); 
     if (num2 < 0) ; 
     { 
      Console.WriteLine("The number cannot be negative"); 
      Console.ReadKey(); 
      return; 
     } 
     double x = num; 
     double y = num2; 
     Console.WriteLine("Base is " + x + "cm long, and the height is " + y + "cm"); 
     Console.WriteLine("1/2*" + x + "*" + y); 
     Console.WriteLine("The area is " + 0.5*x*y + "square cm."); 
     Console.ReadKey(); 
     } 
    } 
} 

回答

12

從您的if語句中刪除分號。

if (num2 < 0) ; 

應該

if (num2 < 0) 
+0

非常感謝你,它的工作原理:d – Patryk

+0

不客氣。如果您對此感到滿意,請選擇此作爲接受的答案。 – RJM

+0

11 upvotes?真?? Sockpuppets任何人? – TaW

相關問題