0

それで、コンピュータークラス用のテキストアドベンチャーゲームを作ろうとしています。私は C# の基本を知っていますが、コードを正しく理解できないため、明らかに何かが欠けています。男性にプレーヤーに質問をさせたいと思います。プレーヤーがノーと答えた場合、ゲームを続行するにはイエスと答える必要があるため、基本的に質問を繰り返します。for ループを使用してみましたが、うまくいきませんでした。とにかく、これは私が持っているコードです:

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?\"");
            }
        }
    }
}

アイデアが足りなくなったので、私ができることについての助けや提案をいただければ幸いです。

4

4 に答える 4

9

whileループを使用できます:

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

大文字と小文字を区別しないチェックを行う場合は、次のようにします。

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

do-while要件によっては、ループを使用することもできます。

于 2013-05-08T16:40:46.740 に答える
3

do while ループを使用するのが最善の策だと思います。ガイドについては、 MSDN の例を参照してください。

using System;
public class TestDoWhile 
{
    public static void Main () 
    {
        int x = 0;
        do 
        {
            Console.WriteLine(x);
            x++;
        } while (x < 5);
    }
}
于 2013-05-08T16:42:36.167 に答える
0

while ループを使用して、必要な答えが得られるまで繰り返します。

于 2013-05-08T16:40:57.760 に答える
0

このようなもの:

bool isGoodAnswer= false;
while (!isGoodAnswer)
{
// Ask the question
// Get the answer
isGoodAnswer = ValidateAnswer(answer);
}
于 2013-05-08T16:43:11.450 に答える