首页 > 解决方案 > 需要有关 StackOverflowException 错误的帮助:请求的操作导致堆栈溢出

问题描述

我正在尝试制作一个迷你密码游戏以在课程中学习 c#,当我尝试让游戏读取密码时,我不断收到此错误。这是我需要摆脱的代码吗?或者也许添加?如果这有助于我使用统一作为我的引擎。谢谢

// Game state
int level;
enum Screen { MainMenu, Password, Win };

Screen currentScreen;

string password;
// Use this for initialization
void Start()
{
    ShowMainMenu();
}

void ShowMainMenu()
{
    currentScreen = Screen.MainMenu;
    Terminal.ClearScreen();
    Terminal.WriteLine("What would you like to hack into?");
    Terminal.WriteLine("Press 1 for the local library");
    Terminal.WriteLine("Press 2 for the police station");
    Terminal.WriteLine("Enter your selection:");
}
void OnUserInput(string input)
{
    if (input == "menu")
    {
        ShowMainMenu();
    }
    else if (currentScreen == Screen.MainMenu)
    {
        Runmainmenu(input);
    }
    else if (currentScreen == Screen.Password)
    {
        Checkpassword(input);

    }
}



void CheckPassword(string input)
{
    if (input == password)
    {
        checkpassword(input);
        Terminal.WriteLine("Sorry, wrong password!");
    }
    else
    {
        Terminal.WriteLine("WELL DONE!");


    }


}

private void checkpassword(string input)
{
    Checkpassword(input);
}

private void Runmainmenu(string input)
{
    {
        if (input == "1")
        {
            level = 1;
            password = "book";
            StartGame();
        }
        if (input == "2")
        {
            level = 2;
            password = "handcuffs";
            StartGame();
        }
        else if (input == "007")
        {
            Terminal.WriteLine("Please select a level Mr Bond!");
        }
        else
        {
            
        }
    }
}

    void StartGame()
{
    currentScreen = Screen.Password;
    Terminal.WriteLine("You have chosen level " + level);
    Terminal.WriteLine("Please enter your password: ");
}

void Checkpassword(string input)
{
    checkpassword(input);

}

private void runmainmenu(string input)
{
    runmainmenu(input);
    

}

string GetDebuggerDisplay()
{
    return ToString();
}

标签: c#

解决方案


你给自己造成了一个无限循环,这会导致这个错误。

void Checkpassword(string input)
{
    checkpassword(input);

}
private void checkpassword(string input)
{
    Checkpassword(input);
}

推荐阅读