简体   繁体   English

如何在C#中使3个随机数作为数组中的3行并将每个随机数分配给3个变量?

[英]How do I make 3 random numbers in C# as a row of 3 in an array and assign each to 3 variables?

I'm trying to generate 3 random numbers and output it onto a label as an array with 3 in a row. 我正在尝试生成3个随机数并将其输出到标签上,作为连续3个数组。 I am just unsure about doing so. 我只是不确定这样做。 So far, I initialized my random value as 到目前为止,我将我的随机值初始化为

Random valueRandom = new Random();

private void Main()
{
    for (int i = 1; i <= gameAmountInteger; i++)
        DoNextNumber(valueRandom);
}

private void DoNextNumber(Random valueRandom)
{
    int int1;
    int1 = valueRandom.Next(0, 1);
    displayLabel.Text = valueRandom.ToString();
}

It should be (range of 0 - 10) 3, 5, 2 它应该是(0-10的范围) 3, 5, 2

Few things you are doing wrongly: 您做错的一些事情:

valueRandom.Next(0, 1); valueRandom.Next(0,1);

Here the first value of the .Next() method indicates the lower bound and the second parameter indicates the upper bound, and you wanted the upper bound as 10 but given as 1 . 在此.Next()方法的第一个值指示下限,第二个参数指示上限,您希望将上限设置为10但给定为1 As you wanted to get the random number between 0-10 you have to give as valueRandom.Next(0, 10) , which will not include 10 if need to include 10 means have to give it as valueRandom.Next(0, 11) . 当您想要获得0-10之间的随机数时,您必须将其作为valueRandom.Next(0, 10) ,如果需要包含10意味着必须将其作为valueRandom.Next(0, 11) valueRandom.Next(0, 10)将不包括10

displayLabel.Text = valueRandom.ToString(); displayLabel.Text = valueRandom.ToString();

From the DoNextNumber method you are assigning the generated random number to the required UI element, so you will get the last number only in the UI. 通过DoNextNumber方法,您将生成的随机数分配给所需的UI元素,因此您将仅获得UI中的最后一个数字。 If you need to get all the numbers displayed means you have to give it as displayLabel.Text = displayLabel.Text + valueRandom.ToString(); 如果需要获取所有显示的数字,则意味着必须将其指定为displayLabel.Text = displayLabel.Text + valueRandom.ToString();

Consider duplicates 考虑重复

There are possibilities of getting duplicate numbers while executing the .Next() repeatedly, if duplicates will be an issue in your scenario means you have to keep the outcomes into a collection and check for existence before pushing the newly generated random number to the collection. 重复执行.Next() ,有可能会得到重复的数字,如果在您的方案中重复将是一个问题,则意味着您必须将结果保留在一个集合中并检查是否存在,然后才能将新生成的随机数推入该集合。 In this case write the value to the UI after getting the required collection. 在这种情况下,在获取所需的集合后将值写入UI。 In that case you can use the code as like this: displayLabel.Text = String.Join(",", randomCollection); 在这种情况下,您可以使用如下代码: displayLabel.Text = String.Join(",", randomCollection); where randomCollection is the collection which I mentioned above 其中randomCollection是我上面提到的集合

You should be using valueRandom.Next(0, 10) , and I would refactor your code like following: 您应该使用valueRandom.Next(0, 10) ,我将重构代码,如下所示:

public static void Main()
{
    displayLabel.Text = GetRandomsAsString(gameAmountInteger);
}

private string GetRandomsAsString(int numberOfrandoms)
{
    var valueRandom = new Random();
    var randoms = "";
    for (int i = 0; i < numberOfrandoms; i++)
    {
        randoms += valueRandom.Next(0, 10);
    }

    return randoms;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#中显示一行随机数而不是一列? - How do I display a row of random numbers in C# instead of a column? 如何在c#中的数组中反转此文件中数字的每一行 - How can I reverse each row of numbers in this file in my array in c# 如何在C#中的if语句中分配一系列数字? - How do I assign a range of numbers to an if statement in C#? 在 C# 中,如何在数组中存储随机数等于某些总和的次数? - In C#, how do I store the amount of times random numbers equal certain sums in an array? 每次调用构造函数时,如何使C#随机数生成器更改? - How do I make my C# random number generator change with each time the constructor is called? 如何在C#中使用随机数? - How do I use random numbers in C#? 如何在C#中使这项工作成为随机数 - How to make this work random numbers in C# 如何生成随机的 1 位数字,然后将它们添加到 c# 窗口窗体应用程序中 datagridview 的每个单元格中? - How do i generate random 1 digit numbers and then add them in each cell of a datagridview in c# window form application? 如何使数据表中的每一行都引用Visual List C#中的项目? - How do I make each Row in a Datatable refer to an item in a Visual List C#? C#:如何将随机 int 值分配给变量? - C#: How to assign random int values to variables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM