如何在C#内计算和显示乒乓球游戏中的点数?

如何在C#内计算和显示乒乓球游戏中的点数?,c#,C#,我在计算我的AI桨叶播放器和用户桨叶播放器每次错过碰撞球时的点数时遇到问题。我尝试使用布尔方法和循环来计数点,但没有用,因为程序不断声明显示我的点的标签是“堆栈溢出”的,因为我无法识别无限循环。你能帮我吗 这是我的密码: private void resetShiruken() { if ((reset == false) && (AIPoints < MAXPoints)) { picShiruke

我在计算我的AI桨叶播放器和用户桨叶播放器每次错过碰撞球时的点数时遇到问题。我尝试使用布尔方法和循环来计数点,但没有用,因为程序不断声明显示我的点的标签是“堆栈溢出”的,因为我无法识别无限循环。你能帮我吗

这是我的密码:

    private void resetShiruken()
    {
        if ((reset ==  false) && (AIPoints < MAXPoints))
        {
            picShiruken.Location = new Point(ClientSize.Width / 2 - picShiruken.Width / 2, ClientSize.Height / 2 - picShiruken.Height / 2); //puts the picShiruken Picturebox in the middle anytime the picPlayerPaddle or picAIPaddle miss and is useful for counting the points of the computer and the player.
            reset = true;
        }
        TheWinner();
   }


  private void TheWinner()
    {
        while (reset == true)
        {
                AIPoints += 1;
                reset = false;

        }
        if (AIPoints >= AIPoints)
        {
            lblAIPoints.Text = AIPoints.ToString();
        }
        resetShiruken();
    }
private void resetShiruken()
{
如果((重置==false)&&(AIPoints=AIPoints)
{
lblAIPoints.Text=AIPoints.ToString();
}
resetShiruken();
}
。resetShiruken()是一种方法,每当有球员失误时,都会将球重置到中间位置

。winner()是一种在任一玩家获得5分后确定游戏赢家的方法

多谢各位

Kai

正如评论所说:

方法的递归调用将永远持续下去
resetShiruken
调用
winner
调用
resetShiruken
,以此类推

    private void resetShiruken()
    {
        if ((reset ==  false) && (AIPoints < MAXPoints))
        {
            picShiruken.Location = new Point(ClientSize.Width / 2 - picShiruken.Width / 2, ClientSize.Height / 2 - picShiruken.Height / 2); //puts the picShiruken Picturebox in the middle anytime the picPlayerPaddle or picAIPaddle miss and is useful for counting the points of the computer and the player.
            reset = true;
        }
        TheWinner();
   }


  private void TheWinner()
    {
        while (reset == true)
        {
                AIPoints += 1;
                reset = false;

        }
        if (AIPoints >= AIPoints)
        {
            lblAIPoints.Text = AIPoints.ToString();
        }
        //resetShiruken();  <--- if you get rid of that call, should be fine.
    }
private void resetShiruken()
{
如果((重置==false)&&(AIPoints=AIPoints)
{
lblAIPoints.Text=AIPoints.ToString();
}
//resetShiruken();常规答案:
你的函数一直在互相调用。这可能就是溢出的原因

从给定的函数中,我无法准确地推断出程序流是什么样子的,因此我无法为您提供一个修复程序,它将实现您期望的功能

但是,要消除溢出,只需从“resetShiruken”中删除对“TheWinner”的调用,或者从“thewiner”中删除对“resetShiruken”的调用

我想你应该把后者去掉


代码样式: 您混合了大小写函数名,这是一个坏习惯。

方法名称的默认大小写是c#(如所示)。

TheWinner
调用
resetShiruken
resetShiruken
调用
TheWinner
-这是无限循环
TheWinner()
调用
resetShiruken()
resetShiruken()
调用
TheWinner()
TheWinner()
调用
resetShiruken()
resetShiruken()
调用
TheWinner()
TheWinner()
调用
resetShiruken()
调用
TheWinner()
。注意:“无限循环”和“无限递归”是两个不同的东西。您有一个无限递归。与循环无关。还要注意,堆栈溢出异常的堆栈跟踪将告诉您问题所在。查看堆栈跟踪,您将看到两个方法无休止地相互调用。