如何重复执行相同的代码,直到播放器回答不同? [英] How can I make the same code repeat until the player answers differently?
问题描述
因此,我正在尝试为我的计算机课制作一款文字冒险游戏.我知道C#的基础知识,但显然我遗漏了一些东西,因为我无法正确编写代码.我想让这个人问玩家一个问题,如果他们回答否",则基本上是重复该问题,因为他们必须回答是"才能继续游戏.我尝试使用for循环,但效果不佳.无论如何,这是我的代码:
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.
推荐答案
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);
}
}
这篇关于如何重复执行相同的代码,直到播放器回答不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!