如果数字为负数,我希望程序关闭,但即使数字为正,它也会关闭

时间:2016-04-29 18:28:20

标签: c#

所以基本上我编写了一个计算三角形面积的小程序。但是,即使输入正数,我也会收到警告,即该数字为负数。在我添加负数检查之前它工作了。有人可以帮帮我吗?

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

1 个答案:

答案 0 :(得分:12)

if语句中删除分号。

if (num2 < 0) ;

应该是

if (num2 < 0)