c#中随机数的总和/平均值

时间:2015-01-07 06:49:32

标签: c# random numbers sum average

我是编程新手,如果有人可以帮我解决以下问题,我会很高兴:“编写一个程序,随机选择值为0到9的整数。程序将计算随机数的平均值和打印有多少随机数大于平均值。程序的用户将指定随机生成的数量“。你如何得到随机数的总和,这样你才能得到平均值?这是我到目前为止所得到的:

int ChosenRandom;

Console.Write("Choose a number between 0-10; ");  

ChosenRandom = int.Parse(Console.ReadLine()); 

Random rnd = new Random();

int RandomNumber = rnd.Next(0, 10);

for (int i = 0; i < ChosenRandom; i++)

{
    Console.WriteLine("Random numbers: " + rnd.Next(0, 10));
}

int TotalRandom;

TotalRandom = ChosenRandom + (RandomNumber); 

Console.WriteLine("Total is:" + TotalRandom);

int avr;

avr = TotalRandom / ChosenRandom;     

Console.WriteLine("Average is: " + avr);

if (ChosenRandom > avr)    
{
    Console.WriteLine("Numbers larger than average" + ChosenRandom);
}

else   
{
    Console.WriteLine("All numbers under average");
}

3 个答案:

答案 0 :(得分:2)

最简单的方法是使用数组,

  1. 在生成数据时将数字存储在数组中

  2. 使用数组元素查找总数和平均值

  3. 遍历数组,将每个元素与平均值

  4. 进行比较

答案 1 :(得分:0)

检查此解决方案是否对您有帮助。

我使用linq创建平均值并找到高于“平均值”的所有数字。

using System;
using System.Linq;

namespace Test
{
    class Program
    {
        static void Main()
        {
            int chosenRandom;
            Console.WriteLine("Choose a number between 0-10");
            chosenRandom = int.Parse(Console.ReadLine());

            Random rand = new Random();
            double[] randomNumbers = new double[chosenRandom];
            for(int i = 0; i < chosenRandom; i++)
            {
                Console.WriteLine("Random numbers: " + (randomNumbers[i] = rand.Next(0, 10)));
            }

            double average = randomNumbers.Average(t => t);

            var numbersAboveAverage = randomNumbers.Where(t => t > average);
            Console.WriteLine("Average of all random numbers - {0}", average);

            foreach(var number in numbersAboveAverage)
                Console.WriteLine(number);

        }
    }
}

答案 2 :(得分:0)

你的程序看起来不错。但是,你以错误的方式理解了这个问题!问题是随机整数的值应该是0-9。不是随机数。随机数的数量可以是用户给出的任何值。

请在下面找到完整的实施方案。

    class Program
{
    static void Main(string[] args)
    {
        //Step 1. Get the no of random numbers (n) to be generated from user. 
        Console.WriteLine("Enter the no of Random numbers: ");
        int n = int.Parse(Console.ReadLine());  

        //Step 2. Generate 'n' no of random numbers with values rangeing from 0 to 9 and save them in an array.
        Random rand = new Random();
        int[] randCollection = new int[n];  
        for (int i = 0; i < n; i++)
        {
           randCollection[i] = rand.Next(9);
           Console.WriteLine("Random No {0} = {1}", i + 1, randCollection[i]); 
        }

        //Step 3. Compute Average
        double average = randCollection.Average();
        Console.WriteLine("Average = {0}", average); 

        //Step 4. Find out how many numbers in the array are greated than the average. 
        int count = 0; 
        foreach(int i in randCollection){
            if (i > average) count++; 
        }
        Console.WriteLine("No of random numbers above their average = {0}", count);
        Console.ReadLine(); 
    }
}

希望这会有所帮助。如果您有任何疑问,请告诉我。