c# - 我希望程序在数字为负数时关闭,但即使数字为正数它也会关闭

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

最佳答案

if 语句中删除分号。

if (num2 < 0) ;

应该是

if (num2 < 0)

关于c# - 我希望程序在数字为负数时关闭,但即使数字为正数它也会关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36945208/

相关文章:

c# - LINQ 中 select 和 where 之间的区别

c# - 隐藏或向变量添加新行

c# - 使用 EmguCV 的 BGR 直方图比较

c# - AutoMapper,如何保持映射对象之间的引用?

c# - 无法让 C# 中的 SslStream 接受 .net Framework 4.6 的 TLS 1.2 协议(protocol)

c# - 通过 ref 和实例化新对象时的堆分配

c# - 数字增量太多

c# - SaveChanges 后如何从数据库更新缓存的 EF?

c# - .net 字符串缓存/池

javascript - 在 C# block 内使用 javascript 变量