简体   繁体   English

C#数学百分比问题

[英]C# Math percentage issue

I have a problem in calculating percentage. 我在计算百分比时遇到问题。 I couldn't work out what is the issue. 我无法解决问题所在。 The problem is the 'perc' variable used in the showData method, but I've included the whole source code. 问题是在showData方法中使用的'perc'变量,但是我已经包含了整个源代码。 It just keeps outputting zero. 它只是保持输出零。 Thanks in advance. 提前致谢。

This is my code‌: 这是我的代码‌:

using System;
using System.Linq;

namespace Task6_5
{
class Traffic
{
    int[] trafficCount;
    int max = 0;
    int total = 0, index;
    double perc;

    public static void Main (string[] args)
    {
        Traffic myTraffic = new Traffic ();
        myTraffic.report ();
    }
    public Traffic() {
        trafficCount = new int[5];
    }
    public void enterCounts() {
        for (int i = 0; i < 5; i++) {
            Console.WriteLine ("Please enter Car Count No. " + (i + 1) + " : ");
            trafficCount [i] = Convert.ToInt32(Console.ReadLine ());
        }
    }
    public void showTotal() {
        for (int i = 0; i < trafficCount.Length; i++) {
            total += trafficCount [i];
        }
    }
    public void busiest() {
        max = trafficCount.Max ();
        index = Array.IndexOf (trafficCount, max) + 1;
        Console.WriteLine ("Busiest Hour : " + index);
    }
    public void showData() {
        Console.WriteLine ("Traffic Report");
        Console.WriteLine ("====================");
        Console.WriteLine ("Hour        Car Count       Percentage Total");
        Console.WriteLine ("================================================");
        for (int i = 0; i < trafficCount.Length; i++) {
            perc = trafficCount[i] / total * 100;
            Console.WriteLine ((i + 1) + "              " + trafficCount [i] + "         " + perc + "%");
    }
        Console.WriteLine ("Total Number of cars seen today : " + total);
}
    public void report() {
        enterCounts ();
        showTotal ();
        showData ();
        busiest ();
    }
}
}

you should change this to double because int / int return an int in c# 您应该将其更改为double,因为int / int在c#中返回int

 double [] trafficCount;
    double total = 0; 

or you can just 或者你可以

((double)trafficCount[i]) / ((double)total * 100.0f);

Integer division yields integers in c#. 整数除法在c#中产生整数。 You can re-order your expression to correct for it though: 但是,您可以对表达式重新排序以进行更正:

perc = trafficCount[i] * 100 / total;

You statement: 您声明:

perc = trafficCount[i] / total * 100;

is working with integer data type. 正在使用整数数据类型。 Although you have defined perc as double but the right hand side of the assignment is all integer and hence the calculation is being done in integer type. 尽管您已将perc定义为double,但是赋值的右侧都是全整数,因此计算以整数类型完成。 You need at least one of the operand to be of type double/float to get a floating point number. 您需要至少一个操作数的类型为double / float才能获得浮点数。 like: 喜欢:

perc = ((double)trafficCount[i] / total) * (double)100;

In your current code all the calculations are being done in integer type , thus resulting an integer value so for something like 0.12 you would end up with 0 . 在您当前的代码中,所有计算都是以整数类型完成的,因此会得出一个整数值,因此对于0.12结果,最终将为0 By casting your operands to double you inflict floating point asthmatics. 通过将操作数强制转换为两倍,会导致浮点哮喘。 You can either cast to double or use 100d 您可以转换为加倍或使用100d

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM