小编Hat*_*teB的帖子

C#计算int数组中值的平均值

当我尝试在int数组中打印值的平均值时会出现什么问题,并且它打印出的值与我的值完全不同.

int[] numbers;
numbers = new int[5];
Console.WriteLine("give five integer numbers:");
numbers[0] = Int32.Parse(Console.ReadLine());
numbers[1] = Int32.Parse(Console.ReadLine());
numbers[2] = Int32.Parse(Console.ReadLine());
numbers[3] = Int32.Parse(Console.ReadLine());
numbers[4] = Int32.Parse(Console.ReadLine());
int sum = 0;

foreach (int x in numbers) {
    sum += x;
    int aver = sum / numbers.Length;  
    Console.WriteLine("average: {0}",aver);
}
Run Code Online (Sandbox Code Playgroud)

c# arrays mean

1
推荐指数
2
解决办法
1万
查看次数

C# 创建 3 个数组并用另外两个数组填充第三个数组

我需要创建三个数组。一个是int,其他是十进制。每个数组都可以有三个项目的空间。ProductPrice(decimal) 数组需要填充产品价格。ProductAmount(int) 需要填写产品的数量。ProductTotalPrice(decimal) 需要填充与其他两个数组的项执行的乘法结果。最后,我需要打印产品名称和总价,如下所示:

产品 1:9.90

产品 2:77.0

产品 3:95.50

这是某种不起作用的代码,但我不知道如何完成任务。

 using System;
class calculation
{
    static void Main(string[] args)
    {
        decimal[] productPrice;
        decimal[] productTotalPrice;
        productPrice = new decimal[3];
        int[] productAmount = { Product_1, Product_2, Product_3 };
        productTotalPrice = new decimal[3];
        productPrice[0] = 9.90m;
        productPrice[1] = 77.0m;
        productPrice[2] = 95.50m;
        productTotalPrice[0] = productAmount[0] * productPrice[0];
        Console.WriteLine(productTotalPrice[0]);
    }
}
Run Code Online (Sandbox Code Playgroud)

c# arrays

0
推荐指数
1
解决办法
342
查看次数

标签 统计

arrays ×2

c# ×2

mean ×1