变量内部循环总是返回零

时间:2015-03-28 10:55:24

标签: c# loops

作业如下:

“编写一个接受任意数量的月销售额的程序。显示值的总和。显示一个报告,显示输入的每个原始值以及该值对总数的贡献百分比。您可以提示用户输入的数量。要输入的值。“

代码在我的“百分比”变量之外运行正常,在列出销售额和总数百分比时总是回归为零......我的4AM大脑难以接受。

static void Main(string[] args)
    {
        int arraylength;
        string inValue;
        Console.WriteLine("Input desired length of array");
        inValue = Console.ReadLine();
        if  (int.TryParse(inValue,  out  arraylength)  ==  false)
            Console.WriteLine("Invalid  data  entered  -  " +
            "0 recorded for number of sales.");
        int [] sales = new int [arraylength];
        for (int i = 0; i < arraylength; i++)
        {
            inValue = Console.ReadLine();
            sales[i] = int.Parse(inValue);
        }
        int sum = 0;
        for (int i = 0; i < arraylength; i++)
        {
            sum += sales[i];
        }
        for (int i = 0; i < arraylength; i++)
        {
            int percent = sales[i] / sum;
            Console.WriteLine("Sales amount is " + sales[i]);
            Console.WriteLine("Percentage of total is " + percent);
        }
        Console.ReadKey();

       }

0 个答案:

没有答案
相关问题