C#不了解此代码... [英] C# Not understanding this code...............

查看:94
本文介绍了C#不了解此代码...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能帮我这是我的代码吗?

Can some please help me this is my code

// the death movment
private void timer2_Tick(object sender, EventArgs e)
{
    int movement = (label2.Text == "This Is Level 1") ? 5 : 15;
    // level 1
    if (label2.Text == "This Is Level 1")
    {
        if (Death.Width == 64)
        {
            Death.Location = new Point(Death.Location.X + movement, Death.Location.Y);
        }

        if (Death.Location == new Point(390, 202))
        {
            Death.Width = 63;
        }
        if (Death.Width == 63)
        {
            Death.Location = new Point(Death.Location.X - movement, Death.Location.Y);
        }

        if (Death.Location == new Point(30, 202))
        {
            Death.Width = 64;
        }
    }
    // level 2
    if (label2.Text == "This Is Level 2")
    {
        if (Death.Width == 64)
        {
            Death.Location = new Point(Death.Location.X + movement, Death.Location.Y);
        }

        if (Death.Location == new Point(390, 202))
        {
            Death.Width = 63;
        }
        if (Death.Width == 63)
        {
            Death.Location = new Point(Death.Location.X - movement, Death.Location.Y);
        }

        if (Death.Location == new Point(30, 202))
        {
            Death.Width = 64;
        }
    }

}



我试图在另一篇文章中问这个问题,但是我没有答案,所以无论如何,我不知道这部分是做什么的.



I tried asking this in another post but i got no answer, so anyway, i dunno what this part does.

int movement = (label2.Text == "This Is Level 1") ? 5 : 15;



在这一部分



at this end part

? 5 : 15;

推荐答案

查看
See what the documentation says[^]. :)

After reading the documentation about the conditional operator
(?:)

的文档后,您将发现,如果label2的文本与"This Is Level 1"完全相同,则movement将为5,否则movement将为15.因此,您可以重写与以下行相同:

you will figure out that if the text of label2 is exactly equal to "This Is Level 1" then movement will be 5 otherwise movement will be 15. So you can rewrite the same line as:

int movement = 15; // Initially 15

// Check if we need to assign 5 as value
if(label2.Text == "This Is Level 1")
  movement = 5;


看到不需要else块,因为我选择用15初始化movement,我希望现在一切都清楚了. :)


See that there is no need for else block, because I choose to initialize movement with 15. I hope it is all clear now. :)


int movement = (label2.Text == "This Is Level 1") ? 5 : 15;



OR



OR

int movement = 0;
if(label2.Text == "This Is Level 1")  
   movement = 5;
else 
   movement = 15;



谢谢,
Imdadhusen



Thanks,
Imdadhusen


int movement = 15;
if (label2.Text == "This Is Level 1")
    movement = 5;


这篇关于C#不了解此代码...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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