C#:在1-1000之间生成100个随机数并输出最大值

问题描述 投票:0回答:5

我对编码很新,我无法绕过循环/阵列/ Randoms。我理解这个概念但是在应用它时,我只是迷失了。

在这里,我试图在1-1000之间生成100个随机数,并且必须输出最大值。到目前为止,这是我的代码:

Random rnd = new Random();
int nums = rnd.Next(0, 1001);
for (int i = 1; i <= 100; i++)
{

}
Console.WriteLine(nums);
Console.ReadLine(); 

它只给我一个号码。 :(我非常感谢任何帮助!

谢谢!

c# random generator
5个回答
1
投票

您可以将随机生成的数字累积到数组中,然后通过使用数组的Max函数,您可以找到最大值

class Program
{
    public static void Main(string[] args)
    {
        Random rnd = new Random();
        int[] intArr = new int[100];

        for (int i = 0; i < intArr.Length; i++)
        {
            int num = rnd.Next(1, 1000);
            intArr[i] = num;
            Console.WriteLine(num);
        }

        Console.WriteLine();
        int maxNum = intArr.Max();
        Console.WriteLine("The max num is:" + maxNum);
        Console.ReadLine();
    }
}

Click to watch online demo


0
投票

你需要在循环内调用rnd.Next()。

Random rnd = new Random();
for (int i = 1; i <= 100; i++)
{
    int nums = rnd.Next(0, 1001);
    Console.WriteLine(nums);
}

Console.ReadLine(); 

0
投票

一个好方法是初始化一个存储最大值的变量。然后在迭代块中生成一个随机数,如果它大于最大值,则将其设置为新的最大值。

Random r = new Random();
int max = 0; //declare our max variable
for(int i = 0; i < 100; i++)
{
    int rand = r.Next(0, 1001);
    if(rand > max) //if the new random value is greater than our max, set max = rand
        max = rand;
}

Console.WriteLine(max); //Output the maximum value
Console.ReadLine(); 

如果要输出每个随机值,然后输出生成的所有值中的最大值,只需通过在循环中输出rand来修改上面的代码。

希望这可以帮助!


0
投票

我不确定,你问这样吗?

Random random = new Random();

int[] nums = new int[100];

// when for loop ends, nums are full of 100 numbers
for (int i = 0; i < nums.Length; i++)
{
    int newNum = random.Next(1, 1000);

    // show every number
    Console.WriteLine(newNum);

    nums[i] = newNum;
}

// get the max number
var maxNum = nums.Max();

Console.WriteLine(maxNum);

0
投票

如果你想看到Loops / Arrays / Randoms的代码都在一起工作,你可以使用下面的注释来逐步了解每条线的作用(Working .NET Fiddle Example

public static void Main()
{
    // Pass in what range we want our randomly generated numbers to be in
    // In your case, between 1 - 1000 and we want to create 100 of them.
    //(See GenerateRandomNumbers())

    var random = GenerateRandomNumbers(1, 1000, 100);

    //Take our newly returned randomly created numbers and 
    //pass them to our GetMaxNumber method so it can find the Max number
    //See (GetMaxNumber())

    var result = GetMaxNumber(random);

    //We now have our max number; print it to the Console.

    Console.WriteLine("Max: " + result);
}

public static int GetMaxNumber(params int[] inputs)
{
    //Create a variable that will store the largest number we find in our array

    int max = inputs[0];

    //Iterate (loop) through all of the 100 values in our array that we passed in
    //Here we define "input" which will hold the value for each value in inputs as we check
    //if the value of input is greater than our current value of max. If it is greater than our
    //current value of max, then we need to update max to now be equal to the value of our input.
    //Note: it will do this comparison 100 times beginning with the first value in the inputs array

    foreach (var input in inputs)
    {
        if (input > max)
        {
            //input's value is greater than the current value of max; update max so that it is equal to the current value of input.

            max = input;
        }
        //no more code; return to top of foreach loop and set input to the next value in inputs
    }

    //When we get here, it means our foreach loop has completed going through and comparing all 100 values of inputs to see which value is the largest.
    //now return this value to Main()

    return max;
}

public static int[] GenerateRandomNumbers(int beginRange, int endRange, int maxNumbers)
{
    // Instantiate random number generator

    Random rnd = new Random();

    //Generate and display 

    int[] intArr = new int[maxNumbers];

    //Generate 100 numbers with numbers between 1 and 1000

    for (int i = 0; i < intArr.Length; i++)
    {
        int num = rnd.Next(beginRange, endRange);
        intArr[i] = num;
    }

    return intArr;
}
© www.soinside.com 2019 - 2024. All rights reserved.