【问题标题】:Possible Mistaken Empty Statement C#可能错误的空语句 C#
【发布时间】:2015-04-08 00:02:35
【问题描述】:

这是我的代码。我将对应项设置为 20,并且我收到警告可能错误空语句与这些行。我的目标是提高速度,让你走得更难。当我输入数字 20 时也会出现同样的问题。到目前为止,它每次只添加 0.75。

if(speed < counterpart);
            {
                speed += 0.25F;
            }
            if(speed > counterpart);
            {
                 speed += 0.5F;
            }

【问题讨论】:

    标签: c# unity3d game-engine game-development


    【解决方案1】:

    if 语句的末尾删除这些分号。

    因为它们,您总是在花括号中运行这两个语句。你的代码相当于这个:

    if(speed < counterpart);  // empty 'if' statement, doing nothing even if true
    
    speed += 0.25F;
    
    if(speed > counterpart);  // same here
    
    speed += 0.5F;
    

    【讨论】:

    • 感谢您快速回复的帮助。
    【解决方案2】:

    两个块都被执行,因为你的 if 语句后面有分号 (;),所以它们什么都不做。试试这个:

    if(speed < counterpart)
        speed += 0.25F;
    
    else if(speed > counterpart)
        speed += 0.5F;
    

    注意:我还在第二个 if 语句上添加了else,以防止第二个语句在第一个语句成功时执行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-10
      • 1970-01-01
      • 2021-01-11
      • 2015-02-25
      • 2018-01-29
      • 2013-11-21
      相关资源
      最近更新 更多