C#控制台没有正确添加值,我不确定原因:/

时间:2015-04-27 13:23:17

标签: c#

此控制台应用程序用于显示价格,然后以平均格式设置,并且小于NUM且高于NUM,我完全感到困惑。总数和平均数出来的数量恰好不是最小数量和高等数量。

CODE:

            double[] prices = new double[5];
        int count = 0;
        double TotalValues = 0;
        double Average = 0;
        string inputString;
        double lessthanfive = 0;
        double higherthanaverage = 0;
        int x;
        for (x = 0; x < prices.Length; x++)
        {
            count += 1;
            Console.Write("Enter the price for {0}: ", count);
            inputString = Console.ReadLine();
            prices[x] = Convert.ToDouble(inputString);
            TotalValues += prices[x];
            Average = TotalValues / prices.Length;
            if (prices[x] < 5)
                lessthanfive++;

            if (prices[x] > Average)
                higherthanaverage++;
        }
        Console.WriteLine();
        Console.WriteLine("The Sum of The Values Are: {0}", TotalValues.ToString("C2"));
        Console.WriteLine("Numbers Less Than $5.00 Are: {0}", lessthanfive.ToString("C2"));
        Console.WriteLine("The Average of The 20 Prices Are: {0}", Average.ToString("C2"));
        Console.WriteLine("Numbers Higher then Average Are: {0}", higherthanaverage.ToString("C2"));
        Console.ReadLine();

3 个答案:

答案 0 :(得分:2)

在输入最后一个值之前,您无法知道平均值,因此您需要另一个循环来计算高于平均值的项目数:

for (x = 0; x < prices.Length; x++)
{
  count += 1;
  Console.Write("Enter the price for {0}: ", count);
  inputString = Console.ReadLine();
  prices[x] = Convert.ToDouble(inputString);
  TotalValues += prices[x];
  if (prices[x] < 5) {
    lessthanfive++;
  }
}

Average = TotalValues / prices.Length;

for (x = 0; x < prices.Length; x++)
{
   if (prices[x] > Average) {
     higherthanaverage++;
   }
}

答案 1 :(得分:0)

平均值是在错误的范围内(环路内)计算的,因此是高平均值。解决它:

class Program
{
    static void Main(string[] args)
    {
        double[] prices = new double[5];
        int count = 0;
        double TotalValues = 0;
        double Average = 0;
        string inputString;
        double lessthanfive = 0;
        double higherthanaverage = 0;
        int x;
        for (x = 0; x < prices.Length; x++)
        {
            count += 1;
            Console.Write("Enter the price for {0}: ", count);
            inputString = Console.ReadLine();
            prices[x] = Convert.ToDouble(inputString);
            TotalValues += prices[x];
            if (prices[x] < 5)
                lessthanfive++;
        }

        Average = prices.Average();
        higherthanaverage = prices.Where(price => price > Average).Count();

        Console.WriteLine();
        Console.WriteLine("The Sum of The Values Are: {0:C2}", TotalValues);
        Console.WriteLine("Numbers Less Than $5.00 Are: {0:C2}", lessthanfive);
        Console.WriteLine("The Average of The 20 Prices Are: {0:C2}", Average);
        Console.WriteLine("Numbers Higher then Average Are: {0:C2}", higherthanaverage);
        Console.ReadLine();
    }
}

答案 2 :(得分:0)

你的平均水平会出错,你的计数会因此而失败。计算该循环之外的所有内容也可以使调试变得容易。现在,这不是最优雅的解决方案,但它使用List来利用内置的Sum和Average函数,以及允许列表重新调整大小,以防您不想添加每次20个号码。

        List<decimal> prices = new List<decimal>();
        int numPrices;
        decimal totalPrice;
        decimal averagePrice;
        string inputString;
        int lessThanFive = 0;
        int higherThanAverage = 0;

        Console.Write("Enter the number of prices that you will be entering: ");
        numPrices = Convert.ToInt32(Console.ReadLine());

        for (int i = 0; i < numPrices; i++)
        {
            Console.Write("Enter the price for item #{0}: $", i+1);
            inputString = Console.ReadLine();
            prices.Add(Convert.ToDecimal(inputString));
        }

        totalPrice = prices.Sum();
        averagePrice = prices.Average();

        foreach (decimal item in prices)
        {
            if (5 > item)
            {
                lessThanFive++;
            }
            if (averagePrice > item)
            {
                higherThanAverage++;
            }
        }

        Console.WriteLine();
        Console.WriteLine("The sum of the values are: {0:C}", totalPrice);
        Console.WriteLine("The number of prices less than $5.00 are: {0}", lessThanFive);
        Console.WriteLine("The average of the prices entered is: {0:C}", averagePrice);
        Console.WriteLine("The number of prices that are higher than average are: {0}", higherThanAverage);
        Console.ReadLine();

现在,我使用了十进制而不是双精度,因为,在这个例子中它肯定不需要任何东西的双倍,但它可以转换回没有任何问题。还添加了一些小的字符串格式等。主要的是,我检查了数学,它的工作原理。