【问题标题】:How to load elements into array using for loop? C#如何使用for循环将元素加载到数组中? C#
【发布时间】:2012-04-28 17:37:09
【问题描述】:

我是新手,从 iOS 转到 WP7。

我正在生成一个随机数系列,我想将其存储到一个数组中。在 iOS 中是这样的

for(int i=0; i < num; i++) {

        int rand = arc4random_uniform(70);

        if([rand_array containsObject:[NSNumber numberWithInt:rand]]) {

            i--;

        }

我已经搜索过,用谷歌搜索过,但我认为这是我可以提问的地方。请帮助我。

【问题讨论】:

  • 为了让这个问题成为一个建设性的问题,您必须解释循环的作用并就特定问题提出问题。也许您的代码不完整,但我无法弄清楚它的作用。
  • “我是个新手,从 iOS 转到 WP7” - 帮自己一个忙,首先 学习 C#,最好使用控制台应用程序,这样你就知道了 语言在你最终遇到 UI 的所有额外复杂性之前。
  • @Gabe 我得到了 Paul 先生的答复。谢谢先生,因为我很赶,所以我需要这个,
  • 谢谢@JonSkeet,会考虑你的建议。

标签: c# arrays windows-phone-7 for-loop


【解决方案1】:
int min = 1;
int max = 4;
int num = 3;
Random r = new Random();
Int[] ar ;
ar = new Int[num]; // Creates array with 3 palces {ar[0],ar[1],ar[2])
for(i = 0;i =< num - 1;i++) {
ar[i] = r.Next(min,max); // generate random number between 1-4 (include 1 & 4)
}

我认为这应该可行(或者我不明白你的意思)。 祝你好运=]

【讨论】:

    【解决方案2】:

    Enumerable.Range(1, 70) 生成从 1 到 70 的数字。 然后我们像一副纸牌一样洗牌。

    int[] randomNumbers = Enumerable.Range(1, 70).Shuffle(new Random()).ToArray();
    

    这需要在同一文件夹中的单独类中。

    public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source, Random random)
    {
        T[] list = source.ToArray();
        int count = list.Length;
    
        while (count > 1)
        {
            int index = random.Next(count--);
            yield return list[index];
            list[index] = list[count];
        }
        yield return list[0];
    }
    

    这是你想要的吗?

    【讨论】:

      【解决方案3】:

      我看不出你的代码有什么意义。我会使用列表。

      for(int i=0; i < num; i++) 
         {
          int rand = arc4random_uniform(70);//hope you know what you did here, I don't
      
          if(yourList.Contains(rand))
              i--;
          else
              yourList.Add(rand);
          }
      

      如果列表中不包含随机数,则将其添加,否则将重复。

      【讨论】:

      • 感谢您的回答,它在我的项目中帮助了我
      【解决方案4】:

      在 C# 中做这样的事情:

      List<int> numbers = new List<int>();
      
      for ( int i = 0; i < num, i++ ) {
      
          int rand = GetARandomNumber();
          if ( !numbers.Contains( rand ) ) {
              numbers.Add( rand );
          } else {
              i--;
          }
      
      }
      

      您也可以将其转换为 while 循环:

      List<int> numbers = new List<int>();
      
      while ( numbers.Count < num ) {
      
          int rand = GetARandomNumber();
          if ( !numbers.Contains( rand ) ) {
              numbers.Add( rand );
          }
      
      }
      

      【讨论】:

        【解决方案5】:

        很简单,真的!您的代码的直接端口如下所示:

        List<int> rand_array = new List<int>();
        
        
        for(int i = 0; i < num; i++)
        {
            int rand = RandomHelper.GetInt(0, 70);
            if(rand_array.Contains(rand))
            {
                i--; 
                continue;
            }
        
            rand_array.Add(rand);
        }
        

        为了在 C# 中生成随机数,有一个类恰当地称为“Random”。你只需要使用 Random 类的一个实例来生成数字,所以如果你想要这样的东西:

        static class RandomHelper
        {
            static Random rng = new Random(); // Seed it if you need the same sequence of random numbers
        
            public static int GetInt(int min, int max)
            {
                return rng.Next(min, max);
            }
        }
        

        【讨论】:

        • 将静态添加到您的 Random 类变量中。
        • 感谢您的帮助,我现在需要看一下基础知识:(
        猜你喜欢
        • 2019-04-02
        • 2019-05-06
        • 2018-01-27
        • 1970-01-01
        • 2016-10-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-07
        相关资源
        最近更新 更多