简体   繁体   English

由于 StackOverflowExeption 导致进程终止 - C#

[英]Process is terminated due to StackOverflowExeption - C#

I have this problem with my code I cant fix.我的代码有这个问题,我无法修复。

I am making a quiz, but the questions mustnt be asked twice so I avoided that, but when it runs out of options it crashes.我正在做一个测验,但问题不能问两次,所以我避免了,但是当它用完选项时它会崩溃。

It also could be the code after it, but I dont think so.它也可能是它之后的代码,但我不这么认为。

Here is my code:这是我的代码:

private static void chooseQuestion()
{
    Random randomQuestion = new Random();

    int returnValue = randomQuestion.Next(1, 3);

    switch (returnValue)
    {
        case 1:
            if (randomValues.questionOneChosen != 1)
            {
                questionOne();
            }else
            {
                chooseQuestion();
            }
            break;
        case 2:
            if (randomValues.questionTwoChosen != 1)
            {
                questionTwo();
            }else
            {
                chooseQuestion();
            }
            break;
    }

    endQuiz();
}

and here is what is after it:这是它之后的内容:

private static void endQuiz()
{
    Console.Clear();
    Console.WriteLine();
    text.centered("You completed the QUIZ, well done!");
    Console.WriteLine();
    text.centeredWrite("Press ENTER to go back to the menu");
    string input = Console.ReadLine();
    if (input == "")
    {
        Menu.main();
    }else
    {
        endQuiz();
    }
}

If you need more code to help me, please ask me.如果您需要更多代码来帮助我,请询问我。

Thanks in advance!提前致谢!

This is never getting set.这永远不会设置。

randomValues.questionOneChosen

So whatever it is, if it's not 1 or 2 the method is going to call itself again.所以不管它是什么,如果它不是 1 或 2,该方法将再次调用自己。 It's probably always zero.它可能始终为零。

Your chooseQuestion method's logic goes like this:你的chooseQuestion方法的逻辑是这样的:

a. Pick a 1 or 2, randomly
b. If 1, and I haven't asked question 1 yet, show question 1
c. if 1, and I have asked question 1, go to step (a)
d. repeat (b) and (c) for question 2
...

once you answer both questions, you try to get another random question.一旦你回答了这两个问题,你就会尝试得到另一个随机问题。 But there are no more questions available.但没有更多可用的问题。 So you recurse into an exception.所以你递归到一个异常。

Don't write your logic this way.不要这样写你的逻辑。 Use a collection, and remove the item from the collection after you've shown that question.使用集合,并在您展示该问题后从集合中删除该项目。 When the collection is empty, you're done.当集合为空时,您就完成了。

A StackOverflowException on StackOverflow, nice! StackOverflow 上的 StackOverflowException,不错! Does Menu.main() end up calling chooseQuestion()? Menu.main() 最终会调用 chooseQuestion() 吗? When a function is called, a new entry on the stack is created with room for all its local variables and the address to return to when it returns.当一个函数被调用时,堆栈上的一个新条目被创建,为它的所有局部变量和返回时返回的地址留出空间。 If a function calls itself (recursive call), its original stack entry is not released.如果函数调用自身(递归调用),则不会释放其原始堆栈条目。 If it calls itself often enough without returning, the stack will eventually run out of room and give this exception.如果它经常调用自己而不返回,堆栈最终将耗尽空间并给出此异常。 Having said that, the stack is pretty big - I think the default is 1 megabyte - so you need a lot of recursive calls to exhaust it.话虽如此,堆栈相当大 - 我认为默认值是 1 兆字节 - 所以你需要大量的递归调用来耗尽它。

You've got two functions which both call themselves.你有两个函数,它们都调用自己。 If they do it often enough, you'll run out of stack space.如果他们经常这样做,您将耗尽堆栈空间。 Just replace the recursive calls with a loop.只需用循环替换递归调用即可。 Here's your first function:这是您的第一个功能:

private static void chooseQuestion() 
{    
    bool endQuizChosen = false;
    while ( !endQuizChosen ) {
        Random randomQuestion = new Random();
        int returnValue = randomQuestion.Next(1, 3);  
        // ... the rest of the function ...
        endQuizChosen = endQuiz();
    }
}

and edit endQuiz() to not call itself but to return true if user wants to stop and false if they want to go on.并编辑endQuiz()以不调用自身,但如果用户想要停止则返回true ,如果他们想要继续则返回false

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

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