c#将数组计算添加到新的十进制数

时间:2017-03-16 08:00:26

标签: c# arrays sum average

所以问题在于我无法total += parkFee将所有parkFee添加到一起,它似乎需要20 max_fee并将其乘以数组长度(30)而不是采取parkFee的新款。

这很容易让我感到非常轻松,因为我对此很新,但我似乎无法弄明白。
我觉得这个问题可能与if声明有关。如果有人能指出我正确的方向,我将不胜感激。谢谢。

/*pseudocode
Program : CarParkCalc3
     * HOURLY_RATE=2.50
     * MAX_FREE=20.00
     * int[]hoursArray 
     * parkFee = HOURLY_RATE * hoursArray
     * IF parkTime > MAX_FEE THEN
     *      OUTPUT MAX_FEE
     * ELSE
     *      OUTPUT parkFee
     * ENDIF
*/


const decimal HOURLY_RATE = 2.50m;
const decimal MAX_FEE = 20m;
decimal parkFee = 0;
int[] hoursArray = { 8, 24, 9, 7, 6, 12, 10, 11, 23, 1, 2, 9, 8, 8, 9, 7, 9, 15, 6, 1, 7, 6, 12, 10, 11, 23, 1, 2, 9, 8 };


Console.WriteLine("Hours  Park fee");
for (int index = 0; index < hoursArray.Length; index++)

{
    {
        parkFee = HOURLY_RATE * hoursArray[index];

        if (parkFee > MAX_FEE)
        {
            Console.WriteLine("{0,6}   {1,9} ", hoursArray[index], MAX_FEE.ToString("N"));
        }
        else
        {
            Console.WriteLine("{0,6}   {1,9} ", hoursArray[index], parkFee.ToString("N"));
        }

    }

}

decimal total = 0;
double average = 0;
for (int index = 0; index < hoursArray.Length; index++)

{
    total += parkFee;
}

average = (double)total / hoursArray.Length;
Console.WriteLine("Total = " + total);
Console.WriteLine("Average = " + average.ToString("N2"));
Console.ReadKey();

2 个答案:

答案 0 :(得分:0)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
  class Program
{
    static void Main(string[] args)
    {
        {
            /*pseudocode
            Program : CarParkCalc3
                 * HOURLY_RATE=2.50
                 * MAX_FREE=20.00
                 * int[]hoursArray 
                 * parkFee = HOURLY_RATE * hoursArray
                 * IF parkTime > MAX_FEE THEN
                 *      OUTPUT MAX_FEE
                 * ELSE
                 *      OUTPUT parkFee
                 * ENDIF
            */


            const decimal HOURLY_RATE = 2.50m;
            const decimal MAX_FEE = 20m;

            int[] hoursArray = { 8, 24, 9, 7, 6, 12, 10, 11, 23, 1, 2, 9, 8, 8, 9, 7, 9, 15, 6, 1, 7, 6, 12, 10, 11, 23, 1, 2, 9, 8 };
            decimal[] parkFee = new decimal[hoursArray.Length];

            Console.WriteLine("Hours  Park fee");
            for (int index = 0; index < hoursArray.Length; index++)

            {



                {
                    parkFee[index] = HOURLY_RATE * hoursArray[index];

                    if (parkFee[index] > MAX_FEE)
                    {
                        Console.WriteLine("{0,6}   {1,9} ", hoursArray[index], MAX_FEE.ToString("N"));
                    }
                    else
                    {
                        Console.WriteLine("{0,6}   {1,9} ", hoursArray[index], parkFee[index].ToString("N"));
                    }

                }

            }

            decimal total = 0;
            double average = 0;
            for (int index = 0; index < hoursArray.Length; index++)

            {
                total += parkFee[index];
            }

            average = (double)total / hoursArray.Length;
            Console.WriteLine("Total = " + total);
            Console.WriteLine("Average = " + average.ToString("N2"));
            Console.ReadKey();
        }
    }
}

}

你可以用数组做这个只使用一个变量,这就是为什么你得到单值

答案 1 :(得分:0)

移动总计+ = parkFee;上圈。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            /*pseudocode
        Program : CarParkCalc3
             * HOURLY_RATE=2.50
             * MAX_FREE=20.00
             * int[]hoursArray 
             * parkFee = HOURLY_RATE * hoursArray
             * IF parkTime > MAX_FEE THEN
             *      OUTPUT MAX_FEE
             * ELSE
             *      OUTPUT parkFee
             * ENDIF
        */


            const decimal HOURLY_RATE = 2.50m;
            const decimal MAX_FEE = 20m;
            decimal parkFee = 0;
            int[] hoursArray = { 8, 24, 9, 7, 6, 12, 10, 11, 23, 1, 2, 9, 8, 8, 9, 7, 9, 15, 6, 1, 7, 6, 12, 10, 11, 23, 1, 2, 9, 8 };
            decimal total = 0;
            double average = 0;

            Console.WriteLine("Hours  Park fee");
            for (int index = 0; index < hoursArray.Length; index++)
            {



                {
                    parkFee = HOURLY_RATE * hoursArray[index];
                    total += parkFee;
                    if (parkFee > MAX_FEE)
                    {
                        Console.WriteLine("{0,6}   {1,9} ", hoursArray[index], MAX_FEE.ToString("N"));
                    }
                    else
                    {
                        Console.WriteLine("{0,6}   {1,9} ", hoursArray[index], parkFee.ToString("N"));
                    }

                }

            }




            average = (double)total / hoursArray.Length;
            Console.WriteLine("Total = " + total);
            Console.WriteLine("Average = " + average.ToString("N2"));
            Console.ReadKey();
        }
    }
}