【问题标题】:StringBuilder not appending int to beginning of string unless 0 or 10StringBuilder 不会将 int 附加到字符串的开头,除非 0 或 10
【发布时间】:2015-07-16 04:37:16
【问题描述】:

我有一个保龄球游戏,它获取保龄球列表,然后通过这段代码运行它们以生成一个穿过 UI 上的帧的字符串。如果用户击球 (0) 或击球 (10),则代码可以正常工作。但是,如果它介于 1 和 9 之间,则无法生成字符串。为什么是这样?我已经搜索并尝试了许多其他方法来做到这一点,但这似乎是唯一可行的方法。

public static StringBuilder FormatRolls (List<int> rolls) {
    StringBuilder output = new StringBuilder();

    for (int i=0; i < rolls.Count; i++) {
        if (rolls.Count >= 19 && rolls[i] == 10) {                                          //Bonus End-Frame Strike
            output.Append ("X");
        } else if (rolls[i] == 0) {                                                         //Gutter
            output.Append ("-");
        } else if (rolls[i-1] + rolls[i] == 10 && rolls.Count > 1) {                                            //Spare
            output.Append ("/");
        } else if (rolls[i] == 10 && rolls[i+1] == 0) {                                     //Strike
            output.Append ("X");
        } else {                                                                            //Normal bowls 1-9
            output.Append (rolls[i].ToString());
        }
    }

    output.ToString();

    return output;
}

这是写入所有帧的代码:

public void FillRolls (List<int> rolls) {
    StringBuilder scoresString = FormatRolls(rolls);
    for (int i=0; i<scoresString.Length; i++) {
        frameText[i].text = scoresString[i].ToString();
    }
}

非常感谢任何帮助,我已经被困了好几天试图让它工作......

【问题讨论】:

  • 你确定你还有其他值吗?
  • 值会在调用脚本之前添加到卷中,因此卷中总会有一些东西。不,不是休息,但我确实有一个 try-catch 方法可以跳过填充字符串,以防 FillRolls() 失败 try { scoreDisplay.FillRolls(rolls); scoreDisplay.FillFrames (ScoreMaster.ScoreCumulative(rolls)); } catch { Debug.LogWarning("FillRollCard failed"); }
  • 对于这样的代码,通常最简单的方法是在 if-else 级联中添加一些 Console.WriteLine 语句并查看执行了哪些分支。在每次循环迭代中输出rolls[i] 的值也有帮助。
  • 在 Unity 3D 中,您可能希望使用 Debug.Log 或 (LogWarning, LogError) 而不是 Console.WriteLine

标签: c# string list unity3d stringbuilder


【解决方案1】:

output.ToString(); 是一个纯函数,您没有使用它的返回值(因此,您正在转换为字符串,然后将该字符串丢弃而不使用/存储/返回它)。我猜你真的想返回完全构建和格式化的字符串,而不是 StringBuilder 实例。使用:

return output.ToString();

也就是说,其他代码路径也不应该产生值。

【讨论】:

  • 另外,你不需要output.Append(rolls[i].ToString())output.Append(rolls[i]) 就足够了,因为 StringBuilder.Append() 的多重重载
  • 谢谢,这让我把public static StringBuilder FormatRolls 改回原来的public static string FormatRolls。在查看参考后,我还更改了Append()。我对此很陌生,谢谢您的帮助!但是,我仍然没有得到输出,除非 StringBuilder 的第一个是 0。连 10 个都没有了……
  • @Magic:你有没有添加调试语句来查看你得到了哪些值以及它在哪里中断?
  • 好吧,摆脱 try-catch 代码使我能够进入调试部分并让我的行通过,无论它是否有效......现在重要的是:行 @987654328 @ 是它中断的地方,因为 rolls[i-1] 不存在,它会抛出 ArgumentOutOfRangeException,但是当我还检查 rolls.Count &gt; 1 时它为什么会这样做?
  • 要是我早点注释掉 try-catch 方式就好了……这个问题现在很不一样了……这是有道理的,当只有 0 会通过其他一切都会起作用时,因为然后rolls[i-1] 实际上就在那里,它会在代码中断之前首先检查 0。否则它会抛出异常并中断。我觉得很傻......@CharithJ 在他上面的评论中是对的,我飞过它。
【解决方案2】:

在浏览了其他人的所有 cmets 和建议后,我删除了阻止我查看 catch 错误的 try-catch 部分。然后,添加 debug.log 语句来帮助找到正确的位置,并使用 Unity 的内置错误来发现这根本不是字符串的错。它试图找到一个不存在的索引rolls[i]-1。经过几次尝试,我发现如果我将else if (rolls[i-1] + rolls[i] == 10 &amp;&amp; rolls.Count &gt; 1) 更改为嵌套的if 语句并使用output.Length 而不是rolls.Count,它将不再返回错误。感谢所有试图帮助我解决这个问题的人,你为我指明了正确的方向,真的让我思考!这是完成的代码:

public static string FormatRolls (List<int> rolls) { StringBuilder output = new StringBuilder();

    for (int i=0; i < rolls.Count; i++) {
        if (rolls.Count >= 19 && rolls[i] == 10) {                                          //Bonus End-Frame Strike
            output.Append ("X");
        } else if (rolls[i] == 0) {                                                         //Gutter
            output.Append ("-");
        } else if (rolls[i] == 10 && output.Length % 2 == 0) {                              //Strike
            output.Append (" X");
        } else if (output.Length >= 1 && output.Length % 2 != 0) {                                                  //Spare
            if (rolls[i-1] + rolls[i] == 10) {
                output.Append ("/");
            }
        } else {                                                                            //Normal bowls 1-9
            output.Append (rolls[i]);
        }
    }

    return output.ToString();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    • 1970-01-01
    • 2020-11-18
    • 2016-04-05
    • 2018-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多