在我的猜谜游戏中创建一个循环

时间:2015-11-07 22:31:30

标签: c#

如果玩家得到错误答案,我该如何重复此代码/游戏?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Random RandomGenerator = new Random();// random number generator
            Console.WriteLine("i have thought of a number between 1 and 100");//display message
            //
            //
            int IN_RandomNum = RandomGenerator.Next(1, 100); //the range
            //loop 10 times 
            for (int i = 0; i < 10; i++) //for loop created
            {
                int IN_Guess; //guessing integer
                Console.Write("{0} turns left, enter your next guess>", 10 - i);//number of turns players has 
                //
                IN_Guess = Convert.ToInt32(Console.ReadLine()); //string to number
                //Now check if guess is same as generated 
                if (IN_Guess == IN_RandomNum)//if guess is equal to generated number
                {
                    Console.WriteLine("correct in {0} turns", i + 1); //if guessed correctly 
                    break; //breaking code
                }
                else if (IN_Guess > IN_RandomNum) //if guess is higher than generated number
                {
                    Console.WriteLine("Too high");// if guessed number is too high
                }
                else //then...
                {
                    Console.WriteLine("Too low"); // if guessed number is too low
               } if (i==8) //on last turn display this message
                {
                    Console.WriteLine("*YOU ONLY HAVE 1 GUESS LEFT!*"); //display this message 
                }

            }

            Console.WriteLine("please press enter to quit"); //display message
            Console.ReadLine();//keeps application open until enter button hit
        }
    }
}

请帮助,因为我真的无法弄清楚在哪里放置while循环...我已经没有了想法

1 个答案:

答案 0 :(得分:0)

你需要在while循环中包装所有内容。

当它退出你所拥有的for循环时,无论是赢还是输都没关系,你回到顶部然后重新开始,生成一个新的随机数。

在阅读下面的代码之前,请参阅上面的建议并亲自尝试,看看是否可以破解

这样的事情:

static void Main(string[] args)
{
    Random RandomGenerator = new Random();// random number generator

    while (true)
    {
        Console.WriteLine("i have thought of a number between 1 and 100");//display message
        bool guessedCorrect = false;
        int IN_RandomNum = RandomGenerator.Next(1, 100); //the range
                                                            //loop 10 times 
        for (int i = 0; i < 10; i++) //for loop created
        {
            int IN_Guess; //guessing integer
            Console.Write("{0} turns left, enter your next guess>", 10 - i);//number of turns players has 
                                                                            //
            IN_Guess = Convert.ToInt32(Console.ReadLine()); //string to number
                                                            //Now check if guess is same as generated 
            if (IN_Guess == IN_RandomNum)//if guess is equal to generated number
            {
                Console.WriteLine("correct in {0} turns", i + 1); //if guessed correctly 
                guessedCorrect = true;
                break; //breaking code
            }
            else if (IN_Guess > IN_RandomNum) //if guess is higher than generated number
            {
                Console.WriteLine("Too high");// if guessed number is too high
            }
            else //then...
            {
                Console.WriteLine("Too low"); // if guessed number is too low
            }
            if (i == 8) //on last turn display this message
            {
                Console.WriteLine("*YOU ONLY HAVE 1 GUESS LEFT!*"); //display this message 
            }
        }

        if (guessedCorrect)
        {
            Console.WriteLine("Good job... Lets try again");
        }
        else
        {
            Console.WriteLine("Better luck next timer... here we go");
        }
    }
}

P.S。我每次尝试都输了,你的游戏太难了: - (