【问题标题】:loop though a array then if and then output result?遍历一个数组然后 if 然后输出结果?
【发布时间】:2011-02-06 21:39:17
【问题描述】:

我有个小问题,在下面的代码(C#)中,它循环考虑了数组,然后检查 user_id 的 user_post 是否大于 50,然后写入 user_id,预期结果是

12
13

但实际输出是

12
12
12

代码有什么问题?我尝试了一个标准的 for 循环,但不能正确?

int[] user_id = new int[64];
int[] group_id = new int[64];
int[] user_post = new int[64];

//user 55
user_id[0] = 10;
group_id[0] = 8;
user_post[0] = 4;

//user56
user_id[1] = 11;
group_id[1] = 2;
user_post[1] = 15;

//user57
user_id[2] = 12;
group_id[2] = 2;
user_post[2] = 55;

//user58
user_id[3] = 13;
group_id[3] = 2;
user_post[3] = 56;

foreach (int i in group_id)
{
    if (group_id[i] == 2)
        if (user_post[i] > 50)
            Console.WriteLine(Convert.ToString(user_id[i]));
}

Console.WriteLine("Press any key too continue...");
Console.ReadLine();
// continue...

【问题讨论】:

  • 嗯,这不是一个坏问题,但我认为通过学习使用 IDE 中的调试功能,您可以获得比 SO 更快的答案。
  • +1 完全同意.. 人们将 SO 作为 Debugger 的替代品

标签: c# loops foreach if-statement


【解决方案1】:

因为你有一个只检查 2 的 if 语句

 if (group_id[i] == 2)

其中 "i" 不是计数器,而是来自 foreach 循环的元素。 并且第 2 和第 3 位置的项目有 2 个组 id,所以它总是这样结束:

if (group_id[8] == 2) //false
if (group_id[2] == 2) //true
if (group_id[2] == 2) //true

你的循环应该是这样的:

for(int i = 0 ; i< 64 ; i++)
{
    if (group_id[i] == 2)
    {
        if (user_post[i] > 50)
        Console.WriteLine(Convert.ToString(user_id[i]));
    }

}

【讨论】:

    【解决方案2】:

    在您的 foreach 语句中,i 采用存储在您的 group_id 数组中的值 - 8、2、2、2

    在您的 if 和输出语句中,您使用此值作为数组的索引

    所以,您的 if 语句最终会这样做:

    if(group_id[8])...
    if(group_id[2])...
    if(group_id[2])...
    if(group_id[2])...
    

    您只检查数组中的元素 8 和 2。

    使用for 循环遍历数组索引

    【讨论】:

      【解决方案3】:

      for 语法如下:

      for (int i = 0; // incremental variable
           i < 100;   // determining a limit
           ++i)       // increment the variable
      

      虽然foreach 是这样工作的:

      foreach (var element        // the element itself, not an index
               in
               elementCollection) // iterating through the collection
      

      【讨论】:

        【解决方案4】:

        您正在使用 for each 语句循环错误的数组。您应该像这样使用常规 for 语句循环:

            for (int i = 0;i < user_post.Length; i++)
            {
                if (user_post[i] > 50 && group_id[i] == 2)
                {
                  Console.WriteLine(Convert.ToString(user_id[i]));
                }
            }
        

        【讨论】:

        • 如果我添加另一个问题,例如用户 ID[5] = 15; group_id[5] = 8;用户帖子[5] = 56;那么 15 也显示了,不应该显示?
        • 是的,我已经编辑了代码,只允许考虑 group_id 2。
        【解决方案5】:
        foreach (int i in group_id)
        

        错了,你必须使用:

        for(int i = 0; i < group_id.Length; i++)
        

        因为对于前者,您使用 group_id 中的值作为数组的索引。

        顺便说一句,我建议你创建一个类,例如Info点赞:

        class Info
        {
           public int GroupId {get; set;};
           public int UserId {get; set;};
           public int PostId {get; set;}
        }
        

        这将允许您仅创建一个数组(即Info[])并避免由于 3 个数组的不同长度而可能出现的错误...

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-07-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-04-17
          • 1970-01-01
          相关资源
          最近更新 更多