为什么这段代码不起作用? [英] why doesnt this code work?

查看:61
本文介绍了为什么这段代码不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if (bool232 = true)
            {
                if (gameTime.TotalGameTime.TotalSeconds >= 3)
                {
                    this.Exit();
                }
            }
            else
            {
            }



出于某种原因,这段代码让我的游戏在3秒后退出,尽管bool232是假的,bool232唯一的时间是真的是当屏幕上的游戏显示时

任何帮助?


For some reason this code is making my game exit after 3 seconds despite "bool232" being false, the only time bool232 is true is when the game over screen is shown
any help?

推荐答案

你的意思是:

Don't you mean:
if (bool232 == true)



甚至更好:


or even better:

if (bool232)





(这个布尔值的更好名称也不会受到影响。)



(A better name for that boolean wouldn't hurt, either.)


它应该是这样的:

It should read like this:
if (bool232 == true)
{
    if (gameTime.TotalGameTime.TotalSeconds >= 3)
    {
        this.Exit();
    }
}
else
{
}



你在里面分配一个值if语句而不是比较。代码甚至不应该编译。


You are assigning a value inside the if statement instead of comparing. The code should not even compile.


正如其他人指出的那样,你为你的变量指定了true而不是比较它。



我也同意



As the others pointed out you are assigning true to your variable instead of comparing it.

I also agree that

if (bool232)





优于





is better than

if (bool232 == true)





虽然如果你打算使用==一个好习惯(有时叫做yoda代码)就是总是把常数放在第一位





though if you are going to use the == a good practice (sometimes called yoda code) is to always put the constant first

if (true == bool232)

这样,如果忘记'='符号,你总会遇到编译错误。

that way you will always get a compile error if you forget an '=' sign.


这篇关于为什么这段代码不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆