简体   繁体   English

如何向未初始化的变量添加内容? c#

[英]How do you add something to an uninitialized variable? c#

Im trying to make a cookie clicker style thing where every time you click something it adds 1 to a variable.我试图制作一个 cookie clicker 风格的东西,每次你点击某个东西时,它都会向变量添加 1。 Im trying to do int clicks = clicks + 1 but it says that it is use of an uninitialized variable.我试图做int clicks = clicks + 1但它说它使用了一个未初始化的变量。 I tried to set int clicks = 0 but then it says that clicks is already defined in the scope.我试图设置int clicks = 0但它说 clicks 已经在 scope 中定义。 I tried to see if i could do something like if (clicks == null) but obviously it cant check it because it is not a variable.我试图看看我是否可以做类似if (clicks == null)的事情,但显然它不能检查它,因为它不是一个变量。 I have only used c# for like a day, can someone please help?我只使用了 c# 一天,有人可以帮忙吗?

    private void cookie_Click(object sender, EventArgs e)
    {
        int clicks = 0;
        clicks = clicks + 1;
        numClicks.Text = "" + clicks;
    }

^ this is the code. ^ 这是代码。 i also realized when i click it, it resets itself to 0 and goes back to 1, so it cant go 1, 2, 3 etc. is there a way to set the variable when the form starts and then start to add on clicks?我还意识到,当我单击它时,它会将自身重置为 0 并返回 1,因此它不能 go 1、2、3 等。有没有办法在表单启动时设置变量,然后开始添加点击? im so dumb我太笨了

int clicks = clicks + 1;

Is indeed nonsensical.确实是无稽之谈。 This is the code that is declaring and initializing clicks , so it makes perfect sense that we can't ask "what is the value of clicks ?"这是声明和初始化clicks的代码,所以我们不能问“ clicks的价值是什么?”是完全合理的。 (in order so we can add one to it); (为了让我们可以添加一个); until we have definitely assigned a value to clicks , the value is undefined .直到我们明确地为clicks分配了一个值,该值是未定义的。

Instead:反而:

int clicks = 0;

And then when you want to increment it:然后当你想增加它时:

clicks++;

You'll want to do something like this:你会想做这样的事情:

int clicks = 0;//Define an integer 'clicks' and set it to 0
//and in your click handler:
clicks = clicks + 1;//Increment your count.

Note:笔记:

  • Clicks is an integer, it can never be null. Clicks 是 integer,它永远不会是 null。 (look up c# primitives for more info) (查找 c# 原语了解更多信息)
  • Using int clicks = clicks + 1 doesn't make sense.使用 int clicks = clicks + 1 没有意义。 At the right side of the equation, what's the value of clicks?在等式的右边,点击的价值是多少? It's not defined yet.它还没有定义。

The problem with your implementation is that every time the function gets called the first thing it does is sets clicks to 0. You need to initialize it outside of the function to avoid this.您的实现的问题是,每次调用 function 时,它所做的第一件事就是将点击次数设置为 0。您需要在 function 之外对其进行初始化以避免这种情况。 If you initialize it in the class it will be set to 0 initially when the class is created then you can increase the value everytime the functions is called.如果您在 class 中对其进行初始化,则在创建 class 时最初将设置为 0,然后您可以在每次调用函数时增加该值。 Not sure exactly your use case but without more information I would recommend doing something like this.不确定您的用例,但如果没有更多信息,我建议您这样做。

class YourClass {
    // initialize outside of the function that increases the counter
    int clicks = 0;
    // rest of the code in class

    private void cookie_Click(object sender, EventArgs e)
    {
        clicks++;
        // this would also work: clicks = clicks + 1;
        numClicks.Text = "" + clicks;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM