user2316502
user2316502

Reputation: 49

How can I make the same code repeat until the player answers differently?

So I am trying to make a text adventure game for my computer class. I know the basics of C# but obviously I'm missing something because I can't get the code right. I want to make the man ask the player a question that if they answer no it basically repeats the question because they have to answer yes for the game to continue. I tried using a for loop but that didn't work very well. Anyways, this is the code I have:

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("MINECRAFT TEXT ADVENTURE: PART 1!");
            Console.WriteLine("\"Hello traveller!\" says a man. \"What's your name?\"");
            string playerName = Console.ReadLine();
            Console.WriteLine("\"Hi " + playerName + ", welcome to Minecraftia!\nI would give you a tour of our little town but there really isn't much left to\nsee since the attack.\"");
            Console.WriteLine("He looks at the stone sword in your hand. \"Could you defeat the zombies in the hills and bring peace to our land?\"");
            string answer1 = Console.ReadLine();
            if (answer1 == "yes")
            {
                Console.WriteLine("\"Oh, many thanks to you " + playerName + "!\"");
                answerNumber = 2;
            }
            else if (answer1 == "no")
            {
                Console.WriteLine("\"Please " + playerName + "! We need your help!\"\n\"Will you help us?\"");
                answerNumber = 1;
            }
            else
            {
                Console.WriteLine("Pardon me?");
                answerNumber = 0;
            }
            for (int answerNumber = 0; answerNumber < 2;)
            {
                Console.WriteLine("\"We need your help!\"\n\"Will you help us?\"");
            }
        }
    }
}

Any help or suggestions for what I could do would be greatly appreciated because I've run out of ideas.

Upvotes: 0

Views: 16670

Answers (4)

jgok222
jgok222

Reputation: 414

I think your best bet would be to use a do while loop, check out the MSDN example for a guide

using System;
public class TestDoWhile 
{
    public static void Main () 
    {
        int x = 0;
        do 
        {
            Console.WriteLine(x);
            x++;
        } while (x < 5);
    }
}

Upvotes: 3

Francis.Beauchamp
Francis.Beauchamp

Reputation: 1373

Something like this:

bool isGoodAnswer= false;
while (!isGoodAnswer)
{
// Ask the question
// Get the answer
isGoodAnswer = ValidateAnswer(answer);
}

Upvotes: 0

Matt Burland
Matt Burland

Reputation: 45155

Use a while loop and to keep repeating until you get the answer you want.

Upvotes: 0

Zbigniew
Zbigniew

Reputation: 27604

You can use while loop:

while (answer != "yes")
{
    // while answer isn't "yes" then repeat question
}

If you want to do case insensitive check then:

while (!answer.Equals("yes", StringComparison.InvariantCultureIgnoreCase))
{
    // while answer isn't "yes" then repeat question
}

You may also try using do-while loop, depends on your requirements.

Upvotes: 9

Related Questions