【问题标题】:How to loop through and do diffrent statements while a condition is true?当条件为真时,如何循环并执行不同的语句?
【发布时间】:2016-03-18 10:39:49
【问题描述】:

有没有办法在 while 或 for 循环中为每个循环使用不同的语句? 示例:我有一个 int 数组,长度为 n,我想在每个索引处添加不同的特定值(当然类型相同)...

这样当 index 等于 n-1 时它就会停止。如果它们是一些语句,我知道该怎么做,但是如果要添加 100 个特定值怎么办。

更具体:

public static int[] ArrayPlay(int n) 
{ 
    int[] arr = new int[n]; 
    while (arr[x] < n) 
    arr[0] = 3; 
    arr[1] = 12; 
    arr[2] = 176; 
    arr[3] = 4;
    //so on...
    return arr;
} 

看起来像这样:

public static int[] ArrayPlay(int n)//4
{
    int a = 0;
    int[] arr = new int[n];
    while ( a <= n)  //lets say n is 4
        arr[a] = 3;  //arr[0]
    a++;
        arr[a] = 12; //arr[1]
    a++;
        arr[a] = 4;  //arr[2]
    a++;
        arr[a] = 8;  //arr[3] 
    a++;             //(a=4 Stop!  and return arr)
        arr[a] = 7;
    a++;
        arr[a] = 12;
    a++;
        arr[a] = 22;
    a++;
        arr[a] = 18;

    return arr;
}

它永远不会传递给 a++,因为第一个语句始终为真。

有没有办法在每个语句之间中断、检查条件并继续? 以及检查数组索引 nr 的属性或方法(我写了 arr[x]) x 是要与 n 进行比较的值。

public static int[] ArrayPlay2(int n)//4
{

    int[] arr = new int[n];
    for (int a = 0; a < arr.Length; a++)
    {
        arr[a] = 3;  //arr[0]
        arr[a] = 12; //arr[1]
        arr[a] = 4;  //arr[2]
        arr[a] = 8;  //arr[3] (a=4 Stop!  and return arr)
        arr[a] = 7;
        arr[a] = 12;
        arr[a] = 22;
        arr[a] = 18;

    }
    return arr;
}

为什么上面的代码不起作用??

【问题讨论】:

  • there are 100 specific value to be added 是什么意思?根本不清楚您要达到的目标,因此很难提供帮助。但是有什么反对 if 或 switch 的呢?
  • 不要使用数组。而是使用 List ,您可以在其中添加到末尾,而不是使用数组,您必须在构造数组时指定数组的大小。
  • 停止谈论循环、if 语句、条件。用简单的英语解释你想做什么。
  • 我想用最多 30 个特定值(元素)填充一个数组,元素的数量取决于 int n 参数,因为它决定了数组的长度。

标签: c# arrays loops statements


【解决方案1】:

听起来你只是想copy 数组的一部分。

// TODO: Handle the case "n > magicIntegers.Length" how you see fit.
int[] magicIntegers = new int[] { 3, 12, 4, 8, 7, 12, 22, 18 }; // etc.
int[] copiedArray = new int[n];
return Array.Copy(magicIntegers, 1, b, 0, n);

【讨论】:

    【解决方案2】:

    您的代码中有一个错误 - 虽然只会做 arr[0] = 3; 还是您正在尝试做的事情?

    如果你试图遍历所有元素,为什么不使用:

    int[] arr = new int[n];
    foreach (int i in arr)
    {
    }
    

    如果你想实现它,那么可能:

    for (int i = 0; i < arr.Length; i++)
    {
         arr[i] = ...
    }
    

    我相信你理解错了循环的工作原理 你们所有人:arr[a] = 3; //arr[0] 和 arr[a] = 12; //arr[1] 和 arr[a] = 4; //arr[2] 仍然在单循环内,所以 a 仍然是一个数字。它增加了您在for (int a = 0; a &lt; arr.Length; a++) 部分中写的a++。

    【讨论】:

    • 我知道,我重写了我的问题,所以它专注于我的主要问题如何让我的条件循环将 arr[0]=3 传递给 a++,然后在每个语句之后一次又一次地检查条件。跨度>
    • 看看我的编辑它可能解释了你的错误。
    【解决方案3】:

    您可以使用包含所有可能值的源数组,然后仅将所需的数字复制到新的数组中。

    使用 linq 这很容易:

    public static int[] ArrayPlay(int n)
    {
        return new []{ 3, 12,4, 8, 7,12,22,18 }
            .Take(n)
            .ToArray();
    }
    

    或者您可以使用iterators 一个接一个地为您提供值:

    private static IEnumerable<int> GetValues()
    {
        yield return 3;  //arr[0]
        yield return 12; //arr[1]
        yield return 4;  //arr[2]
        yield return 8;  //arr[3] 
                         //(a=4 Stop!  and return arr)
        yield return 7;
        yield return 12;
        yield return 22;
        yield return 18;
    }
    
    public static int[] ArrayPlay(int n)
    {
        return GetValues()
            .Take(n)
            .ToArray();
    }
    

    【讨论】:

      【解决方案4】:

      我会做这样的事情:

      // your source array
      int[] source = new int[]{3,12,4,8,7,12,22,18}; 
      int n = 4; // or what ever  N you want
      int[] arr = source.TakeWhile(x => x!=n),ToArray(); // with x!=n as your condition to keep taking
      

      希望对你有帮助

      【讨论】:

      • 谢谢你,我在 msdn Array 类库中没有找到 TakeWhile 或 Take 方法,我猜它们是某种 om 接口方法?你能为我解释一下 (x => x !n) 吗?它是一个 linq?
      • 是的,它是一个Lambda 运算符,一个匿名函数。语法是(input parameters) =&gt; expression,在 TakeWhile 中 x 是元素,所以这个方法遍历数组,用 Lambda 表达式测试每个元素,直到返回 true
      • 上面的代码不起作用,但类似的代码:public static int[] ArrayPlay(int n) { return new []{ 3, 12,4, 8, 7,12,22,18 } .Take(n) .ToArray(); } 有效,这是为什么呢?
      • 是的,我的错。是toArray(),take(n)返回的是iEnumrable,不是数组
      • 是的,我添加了 ToArray(),但我很困惑为什么如果我给出参数 n=4 时它只选择前两个元素 3、12
      猜你喜欢
      • 2018-08-05
      • 2015-03-20
      • 1970-01-01
      • 1970-01-01
      • 2021-12-12
      • 1970-01-01
      • 2016-10-29
      • 2018-11-04
      • 1970-01-01
      相关资源
      最近更新 更多