You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#代码生成5×5 string[]数组输出异常求助

问题排查:5×5字符串数组输出元素数量递减的原因

问题描述

我用C#编写代码,意图通过i和j遍历0-4生成5×5的string[]输出,但实际输出每行元素数量递减。输入参数为city1=3、city2=10、city3=12、city4=5,实际输出如下:

<ArrayOfstring xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
  <string>0 30 3 13 25 30 </string>
  <string>0 30 10 22 27 </string>
  <string>0 30 12 17 </string>
  <string>0 30 5 </string>
  <string>0 30 </string>
</ArrayOfstring>

相关代码如下:

public IEnumerable<string> route5(int city1, int city2, int city3, int city4)
{
    int[] city = new int[] { 0, city1, city2, city3, city4 };
    string[] layout = new string[5];
    
    for (int i = 0; i < 5; i++)
    {
        string message = "";
        for (int j = 0; j < 5; j++)
        {
            if (j - i == 0) message= "0"+" ";
            if (j > i)
            {
                if (j - i == 1) message+= (city[j]).ToString()+" ";
                else if (j - i == 2) message+= (city[j] + city[j - 1]).ToString()+" ";
                else if (j - i == 3) message+= (city[j] + city[j - 1] + city[j - 2]).ToString() + " ";
                else  message+= (city1 + city2 + city3 + city4).ToString() + " ";
            }
            else
            {
                if (i - j == 1) message+= (city[i]).ToString()+" ";
                else if (i - j == 2) message+= (city[i] + city[i - 1]).ToString() + " ";
                else if (i - j == 3) message+= (city[i] + city[i - 1] + city[i - 2]).ToString()+ " ";
                else message+= (city1 + city2 + city3 + city4).ToString() + " ";
            }

        }
        layout[i]= message;

    }
    return layout;
}

问题根源

  1. 赋值操作覆盖内容而非追加:当j == i时,代码用message = "0 "直接清空了之前j从0到i-1拼接的所有内容,而不是用+=追加,导致每行前半部分的元素全部丢失,最终呈现元素数量递减的现象。
  2. 条件判断未互斥:三个判断(j-i==0、j>i、else)是独立执行的,没有用else if串联。当j == i时,执行完第一个if的赋值后,会继续进入else分支(因为j不大于i),额外追加了一段内容,导致部分行的元素数量超过预期(比如第一行有6个元素)。

修复后的代码

public IEnumerable<string> route5(int city1, int city2, int city3, int city4)
{
    int[] city = new int[] { 0, city1, city2, city3, city4 };
    string[] layout = new string[5];
    int total = city1 + city2 + city3 + city4; // 提前计算总和,避免重复计算

    for (int i = 0; i < 5; i++)
    {
        string message = "";
        for (int j = 0; j < 5; j++)
        {
            if (j == i)
            {
                message += "0 "; // 改为追加操作,保留之前的内容
            }
            else if (j > i)
            {
                int diff = j - i;
                if (diff == 1)
                    message += city[j].ToString() + " ";
                else if (diff == 2)
                    message += (city[j] + city[j - 1]).ToString() + " ";
                else if (diff == 3)
                    message += (city[j] + city[j - 1] + city[j - 2]).ToString() + " ";
                else
                    message += total.ToString() + " ";
            }
            else
            {
                int diff = i - j;
                if (diff == 1)
                    message += city[i].ToString() + " ";
                else if (diff == 2)
                    message += (city[i] + city[i - 1]).ToString() + " ";
                else if (diff == 3)
                    message += (city[i] + city[i - 1] + city[i - 2]).ToString() + " ";
                else
                    message += total.ToString() + " ";
            }
        }
        layout[i] = message.TrimEnd(); // 可选:移除末尾多余空格,输出更整洁
    }
    return layout;
}

修复说明

  • 将message = "0 "改为message += "0 ",确保保留之前拼接的内容;
  • 用else if串联三个核心条件,保证每个j值只会进入一个分支,避免重复追加内容;
  • 提前计算总和total,减少重复计算,提升代码性能;
  • 可选使用TrimEnd()去掉每行末尾的多余空格,让输出格式更规范。

内容的提问来源于stack exchange,提问作者Flora Jiang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 09:54:50