Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么此代码不显示速度_C# - Fatal编程技术网

C# 为什么此代码不显示速度

C# 为什么此代码不显示速度,c#,C#,我创建了一个向我展示互联网速度的程序。例如,我需要显示26.365 KB/S,但它显示270000000 代码: static void Main(string[] args) { int KB = 1024; int MB = KB * KB; int GB = MB * KB; NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkI

我创建了一个向我展示互联网速度的程序。例如,我需要显示26.365 KB/S,但它显示270000000

代码:

static void Main(string[] args)
{
    int KB = 1024;
    int MB = KB * KB;
    int GB = MB * KB;
    NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();

    foreach (NetworkInterface nic in nics)
    {
        if (nic.NetworkInterfaceType != NetworkInterfaceType.Loopback
            && nic.NetworkInterfaceType != NetworkInterfaceType.Tunnel
            && nic.OperationalStatus == OperationalStatus.Up
            && nic.Name.StartsWith("vEthernet") == false
            && nic.Description.Contains("Hyper-v") == false)
        {
            Console.WriteLine(nic.Description);

            long speed = nic.Speed / KB;
            Console.WriteLine("=====================");
            if (speed < KB)
            {
                Console.WriteLine("{0} Byte", speed);
            }
            else
            {
                if (Convert.ToBoolean(speed < 100))
                {
                    Console.WriteLine("{0} KB/S", speed);
                }
                else if (Convert.ToBoolean(speed < 1000))
                {
                    Console.WriteLine("{0} MB/S", speed);
                }
                else if (Convert.ToBoolean(speed < 10000))
                {
                    Console.WriteLine("{0} GB/S", speed);
                }
                else if (Convert.ToBoolean(speed < 100000))
                {
                    Console.WriteLine("{0} TB/S", speed);
                }
            }
        }
    }
    Console.ReadKey();
}
这不管用。它没有显示我的速度。有什么问题吗?我怎样才能解决这个问题

/************************************************************************/

当前实施

long speed = nic.Speed / KB;
相反,您应该使用支持十进制的数据类型

decimal speed = (decimal)nic.Speed/KB;

问题出在你的身体状况上

如果您的速度大于100000,则没有其他情况下必须执行代码

你应该这样写,如果在最后一个,如果:


此外,您不需要像上面的代码那样转换结果。

在我看来,您缺少一些东西:

根据,Speed属性是每秒返回位,因此在开始比较之前,我们应该将其转换为字节,将其除以8,而不是KB。 在进行比较时,我们应该将速度值与KB、MB、GB和TB的占位符进行比较,而不是与文字数字进行比较。这将减少错误并使其更易于阅读。例如,将速度与10的倍数(而不是1024)进行比较。 您的if语句不能捕获所有条件,只捕获那些小于100000的条件。 我们需要将速度除以显示的大小,在开始时只除以一次,KB 如果要显示小数位数,我们应该在除法之前将数字转换为小数,然后将结果格式化为显示3位小数。 以下是解决这些问题的一种方法:

static void Main(string[] args)
{
    int KB = 1024;
    int MB = KB * KB;
    int GB = MB * KB;
    long TB = (long)GB * KB;

    NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();

    foreach (NetworkInterface nic in nics)
    {
        if (nic.NetworkInterfaceType != NetworkInterfaceType.Loopback
            && nic.NetworkInterfaceType != NetworkInterfaceType.Tunnel
            && nic.OperationalStatus == OperationalStatus.Up
            && nic.Name.StartsWith("vEthernet") == false
            && nic.Description.Contains("Hyper-v") == false)
        {
            Console.WriteLine(nic.Description);

            // 1. Convert bits to bytes
            // 5. Convert result to decimal
            decimal speed = (decimal)nic.Speed / 8;

            // 2. Do comparisons with our variables and
            // 3. Include all possible conditions (starting with > TB)
            if (speed >= TB)
            {
                // 4. Divide the speed by the display size
                // 5. Format the output to show 3 decimal places
                Console.WriteLine("Speed: {0:0.000} TB/S", speed / TB);
            }
            else if (speed >= GB)
            {
                Console.WriteLine("Speed: {0:0.000} GB/S", speed / GB);
            }
            else if (speed >= MB)
            {
                Console.WriteLine("Speed: {0:0.000} MB/S", speed / MB);
            }
            else if (speed >= KB)
            {
                Console.WriteLine("Speed: {0:0.000} KB/S", speed / KB);
            }
            else
            {
                Console.WriteLine("Speed: {0:0.000} Bytes per second", speed);
            }
        }
    }

    Console.Write("\nDone!\nPress any key to exit...");
    Console.ReadKey();
}

为什么是C7标签?您使用了什么新功能?仅此而已,您给了我一个否定点?来自:此属性返回的值由网络接口报告。它不是动态计算的。对我来说,这表明它告诉你的是最大吞吐量,而不是某个连接的测量速度。此外,还可能丢失Convert.ToBoolean调用。根据if语句,该值返回的值大于所有限制,在这种情况下,它不应该显示任何内容。你能验证一下nic.Speed的值是多少吗?另外,似乎你有一些奇怪的限制,如果它小于KB,即1024,你会显示一些东西,但接下来的两个if语句也可能会与该值匹配。由于nic.Speed是一个长Int64,KB是int,所以这不能满足你的要求。它也不可能改变代码的结果。@LasseV.Karlsen但我的数学表明,如果整数除以另一个整数,也可以得到十进制结果。3/2=1.5但在计算中,如果我使用整数来保存结果数字,它将向上或向下取整到最接近的值,所以要保存实际值,应该使用十进制类型。如果我错了,请纠正我。如果你把long除以int,你在C语言中会得到long,大多数编程语言都不介意。要显式获得包含分数值的十进制结果,请在除法之前将至少一个操作数强制转换为十进制。@LasseV.Karlsen如果我们将nic速度强制转换为十进制,将会减少舍入问题,但我认为这与他的问题完全正交。换句话说,一旦他修复了代码的实际问题,这可能会显示更好的分数速度。@kianoush It not show me speed>在我的情况下,您的代码不会显示速度,因为我的接口速度不会影响任何条件。我需要显示26.365 KB/S,但它显示270000000根据您的代码,控制台输出不可能不显示该单元。你真的在执行这个代码吗?我在所有条件下都加了000。它显示263671KB/S@kianoush下载速度与您下载的服务器有关。您的代码不提供此值。它只是提供NIC处理的最大速度。但这不是你所要求的。你问为什么你的代码没有显示速度,这就是为什么。你不需要添加000,你只需要一个简单的else而不是else,如果在最后一个条件下,以确保其中一个会被击中。另外,我只关注为什么您的代码没有显示输出,但看起来您可以简化很多条件。@kianoush现在我如何解决这个问题>这是另一个问题。如果你想解决它,你可以问一个新问题。
static void Main(string[] args)
{
    int KB = 1024;
    int MB = KB * KB;
    int GB = MB * KB;
    long TB = (long)GB * KB;

    NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();

    foreach (NetworkInterface nic in nics)
    {
        if (nic.NetworkInterfaceType != NetworkInterfaceType.Loopback
            && nic.NetworkInterfaceType != NetworkInterfaceType.Tunnel
            && nic.OperationalStatus == OperationalStatus.Up
            && nic.Name.StartsWith("vEthernet") == false
            && nic.Description.Contains("Hyper-v") == false)
        {
            Console.WriteLine(nic.Description);

            // 1. Convert bits to bytes
            // 5. Convert result to decimal
            decimal speed = (decimal)nic.Speed / 8;

            // 2. Do comparisons with our variables and
            // 3. Include all possible conditions (starting with > TB)
            if (speed >= TB)
            {
                // 4. Divide the speed by the display size
                // 5. Format the output to show 3 decimal places
                Console.WriteLine("Speed: {0:0.000} TB/S", speed / TB);
            }
            else if (speed >= GB)
            {
                Console.WriteLine("Speed: {0:0.000} GB/S", speed / GB);
            }
            else if (speed >= MB)
            {
                Console.WriteLine("Speed: {0:0.000} MB/S", speed / MB);
            }
            else if (speed >= KB)
            {
                Console.WriteLine("Speed: {0:0.000} KB/S", speed / KB);
            }
            else
            {
                Console.WriteLine("Speed: {0:0.000} Bytes per second", speed);
            }
        }
    }

    Console.Write("\nDone!\nPress any key to exit...");
    Console.ReadKey();
}