停止For循环并在停止的下一个实例处启动它

时间:2015-03-27 00:29:42

标签: c# loops

所以这个循环是我改变很多事情的第一次尝试。我不确定我是以正确的方式去做的。这是一个rught /错误的答案游戏。每次他们回答错误时,都会调用循环。

在我脑海中,它会转到与numAttempts相关的if。因此,如果numAttempts == 4你会看到"错误的答案2尝试离开!"但每次直到第五次尝试,当循环被调用时,无论numattempts如何,它总是从顶部开始。

为了缓解这种情况,我还尝试在消息检查(错误)代码块中添加numAttempts ++。

我喜欢四个循环的想法,因为基于每个错误的答案,将出现一个不同的图像,基于HangmanImage() - 当前未定义 -

我已尝试在for if语句之间进行中断和返回,但它不起作用。在调用循环来启动numAttempts =的循环时,你能帮我吗? EX。从numAttempts == 2开始?在特定实例完成后停止循环?

我是新编码并试图让它发挥作用。如果我的工作100%错误或我不应该做四看,我感谢你的耐心。读书和网络很棒,但现在又一次,尤其是一开始人们需要指导。请花点时间把我推向正确的方向。

感谢您的时间。

 int numAttempts = (0); // global variable, at the start of the class. This allows the variable to be used anywhere with the current value


    int maxAttempts = (5);


    static void UpdateImage()
    {
        for (int numAttempts = 0; numAttempts < 6; numAttempts++)
        {
            if (numAttempts == (1))
            {
                MessageBox.Show("Wrong Answer 4 tries left!");

                {
                  //  HangmanImage();
                }

            }
            else
                if (numAttempts == (2))
                {
                    MessageBox.Show("Wrong Answer 3 tries left!");

                    {
                //    HangmanImage();
                    }


                }
                else
                    if (numAttempts == (3))
                    {
                        MessageBox.Show("Wrong Answer 2 tries left!"); 
                        {
                 //           HangmanImage()
                        }  

                    }
                    else
                        if (numAttempts == (4))
                        {

                            MessageBox.Show("Wrong Answer 1 try left!"); 

                            {
                              //  HangmanImage()
                            }
                        }
                        else
                            if (numAttempts == (5))
                            {
                                MessageBox.Show("You Lose!");

                                 {
                               //     HangmanImage();
                                 }
                            }
        }

    }

2 个答案:

答案 0 :(得分:0)

这是一个有效的答案:https://dotnetfiddle.net/Z2BaYs

这是代码。基本上,您使用for循环来保持游戏的正常运行,直到用户完成尝试或正确回答为止。

using System;

public class Program
{
    public static void Main()
    {
        var maxAttempts = 5;
        var correctAnswer = "Edam";
        for(int actualAttempts = 1; actualAttempts <= maxAttempts; ++actualAttempts)
        {
            Console.WriteLine("What is the only cheese that's made backwards?");
            Console.WriteLine("Attempt #" + actualAttempts);
            var answer = Console.ReadLine();
            if(answer.Equals(correctAnswer))
            {
                Console.WriteLine("Correct!");
                break;
            }
            switch(actualAttempts)
            {
                case 1: 
                    Console.WriteLine("Whoa. Nice try.");
                    break;

                case 2: 
                    Console.WriteLine("Nope. Wrong.");
                    break;

                case 3: 
                    Console.WriteLine("Incorrect sir!");
                    break;

                case 4: 
                    Console.WriteLine("Still not the correct answer.");
                    break;

                case 5: 
                    Console.WriteLine("...and your done.");
                    break;

                default :
                    break;
            }
        }
    }
}

这是输出/输入的一些示例。

What is the only cheese that's made backwards?
Attempt #1
Cheddar
Whoa. Nice try.

What is the only cheese that's made backwards?
Attempt #2
Mozarella
Nope. Wrong.

What is the only cheese that's made backwards?
Attempt #3
Havarti
Incorrect sir!

What is the only cheese that's made backwards?
Attempt #4
Swiss
Still not the correct answer.

What is the only cheese that's made backwards?
Attempt #5
Edam
Correct!

答案 1 :(得分:0)

我可以说你在学校这样做,所以我不想放弃太多。但是你的问题也是如此,如果我理解你,你想要在循环中保持相同的迭代。只是为了尝试坚持你的代码,你需要传入一个变量并在函数上返回一个变量。喜欢这个

    int updateImage(int count)
    {
        for(count < 6; count++)
        {
            Do what you need;
        }

        return count;
    }

这样你就可以将迭代传入和传出函数。希望这会有所帮助。