簡體   English   中英

C#:在1-1000之間生成100個隨機數並輸出最大值

[英]C#: Generate 100 random numbers between 1-1000 and output the max value

我對編碼非常陌生,只是無法將自己包裹在Loops / Arrays / 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(); 

它只給我一個號碼。 :(非常感謝您的幫助!

謝謝!

您可以將隨機生成的數字累加到數組中,然后通過使用數組的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();
    }
}

點擊觀看在線演示

您需要在循環內調用rnd.Next()。

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

Console.ReadLine(); 

我不確定,你這樣問嗎?

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

一個好的方法是初始化一個存儲最大值的變量。 然后在您的迭代塊內生成一個隨機數,如果它大於您的最大值,則將其設置為新的最大值。

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即可修改上面的代碼。

希望這可以幫助!

如果您想查看所有循環/數組/隨機代碼的協同工作,則可以將下面的注釋與每一行的內容一起使用( 工作.NET小提琴示例

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM