C# 控制台没有正确添加值,我不确定为什么:/

  • 本文关键字:不确定 控制台 添加 c#
  • 更新时间 :
  • 英文 :


这个控制台应用程序应该显示价格,然后将其设置为平均格式,并以小于 NUM 和高于 NUM 的格式进行设置,我完全感到困惑。总数和平均值得出正确的数量,而不是小于五和高于平均水平。

法典:

            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();

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

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++;
   }
}

你的平均值将是错误的,你的计数将因此而下降。 计算该循环之外的所有内容也将使调试变得容易。 现在,这不是最优雅的解决方案,但它使用 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();

现在,我使用这些十进制而不是双精度,因为在这个例子中,它当然不需要双精度,但它可以毫无问题地转换回来。 还添加了一些次要的字符串格式等。 最主要的是,我检查了数学,它有效。

平均值是在错误的范围内(循环内)计算的,高于平均值也是如此。要修复它:

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();
    }
}

相关内容

最新更新