将一个 int 变量添加到 Label 并给它 +1

Adding a int variable to a Label and give it +1

我正忙于迷宫,目前正在 运行 解决这个问题。我希望每当有人碰壁时,他都使用方法 Movetostart,我想让它用 var death 来计算。到目前为止我得到了这个。

int death = 0;
private void MoveToStart()
{
    Count.Text = death.ToString();
    Point startingPoint = panel1.Location;
    startingPoint.Offset(301, 543);
    Cursor.Position = PointToScreen(startingPoint);
    ms = 0;
    sec = 0;
    min = 0;
    hour = 0;
    death = +1;
}

现在的问题是死亡只停留在1,不管撞墙多少次都不算。

我猜你想写 death += 1; 而不是 death = +1;

编辑: 此外,就像其他人评论的那样,您应该在增加值后将值分配给 Count.Text,否则 Count.Text 上的文本将始终落后一步。

您可能想在显示之前增加 death,否则它会显示为 1:

private void MoveToStart()
{
   ++death;
   Count.Text = death.ToString();
   Point startingPoint = panel1.Location;
   startingPoint.Offset(301, 543);
   Cursor.Position = PointToScreen(startingPoint);
   ms = 0;
   sec = 0;
   min = 0;
   hour = 0;
}

使用++运算符会将计数加一并将其分配回对象,与death += 1;相同。

您的代码的实际问题是 death = +1death += 1 不同。您所做的只是将 death 重新分配给 +11.

如果你想给你的"death"变量加1,你可以这样写

int death = 0;
private void MoveToStart()
{
    Count.Text = death.ToString();
    Point startingPoint = panel1.Location;
    startingPoint.Offset(301, 543);
    Cursor.Position = PointToScreen(startingPoint);
    ms = 0;
    sec = 0;
    min = 0;
    hour = 0;
    death++;
}

int death = 0;
private void MoveToStart()
{
    Count.Text = death.ToString();
    Point startingPoint = panel1.Location;
    startingPoint.Offset(301, 543);
    Cursor.Position = PointToScreen(startingPoint);
    ms = 0;
    sec = 0;
    min = 0;
    hour = 0;
    death = death + 1;
}

或者创建另一个变量,每次调用该方法时,您都用它来增加死亡

int death = 0;
int _numberToAddEachTime = 1;
private void MoveToStart()
{
    Count.Text = death.ToString();
    Point startingPoint = panel1.Location;
    startingPoint.Offset(301, 543);
    Cursor.Position = PointToScreen(startingPoint);
    ms = 0;
    sec = 0;
    min = 0;
    hour = 0;
    death = death + _numberToAddEachTime;
}