只陈述一次



我正在Visual Studio C#控制台中进行RPG类型的文本冒险。我正在尝试创建一段代码来检测你的位置。我希望它以必要的形式作出回应。我知道你可能可以用数组更有效地做到这一点,但我对现在的情况很满意。我想做的是阻止它不断重复";它奏效了&";。如果它只声明一次,那么它已经起作用了。我试过多种方法。请为我的问题提供一个解决方案
在此处或下方编码

using System;
namespace RPG
{
class MainClass
{
public static void Main(string[] args)
{
//Done(); will check if Cords are at end of game

//variables
string response = "";
string name = "";
bool hastyped = false;
//Coordinates
int xCord = 0; int yCord = 0;

void Done()
{
if (yCord == 5 && xCord == 7)
{
Console.WriteLine("Thanks for playing! You did it!");
}
}
void Move()
{
if (hastyped == true)
{
if (response == "E")
{
xCord = xCord + 1;
}
else if (response == "W")
{
xCord = xCord - 1;
}
else if (response == "N")
{
xCord = xCord + 1;
}
else if (response == "S")
{
xCord = xCord - 1;
}
else
{
Console.WriteLine("That movement is invalid.");
}
hastyped = false;
response = "";
Done();
}
}

//FANCY START LOGO
Console.WriteLine(@"
____________ _____
| ___  ___   __ 
| |_/ / |_/ / |  /
|    /|  __/| | __ 
| | | |   | |_ 
_| __|    ____/
"
);
System.Threading.Thread.Sleep(2500);
while (response != "Y")
{
Console.WriteLine("Please answer with Y for yes and N for no. Please try to answer all questions as simply as possible.nPlease type N for north, E for east, S for south and W for west.");
Console.WriteLine("Ok?");
response = Console.ReadLine();
if (response == "Y")
{
Console.WriteLine("Well lets start:");
}
else
{
Console.WriteLine("Please try again.");
}
}
//Instructions

Console.WriteLine("LOADING...");
Console.WriteLine("7%");
System.Threading.Thread.Sleep(200);
Console.WriteLine("26 %");
System.Threading.Thread.Sleep(100);
Console.WriteLine("48%");
System.Threading.Thread.Sleep(200);
Console.WriteLine("76%");
System.Threading.Thread.Sleep(600);
Console.WriteLine("87%");
System.Threading.Thread.Sleep(400);
Console.WriteLine("99%");
System.Threading.Thread.Sleep(20);
Console.WriteLine("100%");
//Loading...
Console.WriteLine("Hey buddy, you took that hit pretty hard. Are you alright?");
Console.ReadLine();
Console.WriteLine("Well we need to get you to a hospital.nSorry, what was your name?");
name = Console.ReadLine();
Console.WriteLine("Well " + name + ", I wish we were meeting under better circumstances.");
Console.WriteLine("Anyway, I'm Dave. Off to the East is a path that looks pretty safe, the only other direction is back to the west where I just found you. So we won't go there.");
Console.WriteLine("YOU MOVED EAST!!!");
xCord = xCord + 1;
//Intro

//GAMEPLAY
while (true)
{
//1,0
if (xCord == 1 && yCord == 0)
{
Console.WriteLine("Hit E then enter for testing.");
response = Console.ReadLine();
hastyped = !string.IsNullOrWhiteSpace(response);
if (hastyped == true)
{
Move();
}
}
//2,0
if (xCord == 2 && yCord == 0)
{
Console.WriteLine("It worked.");
}
//3,0
if (xCord == 3 && yCord == 0)
{
}
//-1,0
if (xCord == -1 && yCord == 0)
{
}
//-2,0
if (xCord == -2 && yCord == 0)
{
}
//-3,0
if (xCord == -3 && yCord == 0)
{
}
//1,1
if (xCord == 1 && yCord == 1)
{
}
//2,1
if (xCord == 2 && yCord == 1)
{
}
//3,1
if (xCord == 3 && yCord == 1)
{
}
//-1,1
if (xCord == -1 && yCord == 1)
{
}
//-2,1
if (xCord == -2 && yCord == 1)
{
}
//-3,1
if (xCord == -3 && yCord == 1)
{
}
//1,2
if (xCord == 1 && yCord == 2)
{
}
//2,2
if (xCord == 2 && yCord == 2)
{
}
//3,2
if (xCord == 3 && yCord == 2)
{
}
//-1,2
if (xCord == -1 && yCord == 2)
{
}
//-2,2
if (xCord == -2 && yCord == 2)
{
}
//-3,2
if (xCord == -3 && yCord == 2)
{
}
//1,3
if (xCord == 1 && yCord == 3)
{
}
//2,3
if (xCord == 2 && yCord == 3)
{
}
//3,3
if (xCord == 3 && yCord == 3)
{
}
//-1,3
if (xCord == -1 && yCord == 3)
{
}
//-2,3
if (xCord == -2 && yCord == 3)
{
}
//-3,3
if (xCord == -3 && yCord == 3)
{
}
//1,-1
if(xCord == 1 && yCord == -1)
{
}
//2,-1
if (xCord == 2 && yCord == -1)
{
}
//3,-1
if (xCord == 3 && yCord == -1)
{
}
//-1,-1
if (xCord == -1 && yCord == -1)
{
}
//-2,-1
if (xCord == -2 && yCord == -1)
{
}
//-3,-1
if (xCord == -3 && yCord == -1)
{
}
//1,-2
if (xCord == 1 && yCord == -2)
{
}
//2,-2
if (xCord == 2 && yCord == -2)
{
}
//3,-2
if (xCord == 3 && yCord == -2)
{
}
//-1,-2
if (xCord == -1 && yCord == -2)
{
}
//-2,-2
if (xCord == -2 && yCord == -2)
{
}
//-3,-2
if (xCord == -3 && yCord == -2)
{
}
//1,-3
if (xCord == 1 && yCord == -3)
{
}
//2,-3
if (xCord == 2 && yCord == -3)
{
}
//3,-3
if (xCord == 3 && yCord == -3)
{
}
//-1,-3
if (xCord == -1 && yCord == -3)
{
}
//-2,-3
if (xCord == -2 && yCord == -3)
{
}
//-3,-3
if (xCord == -3 && yCord == -3)
{
}
//0,3
if (xCord == 0 && yCord == 3)
{
}
//0,2
if (xCord == 0 && yCord == 2)
{
}
//0,1
if (xCord == 0 && yCord == 1)
{
}
//0,0
if (xCord == 0 && yCord == 0)
{
}
//0,-1
if (xCord == 0 && yCord == -1)
{
}
//0,-2
if (xCord == 0 && yCord == -2)
{
}
//0,-3
if (xCord == 0 && yCord == -3)
{
}

//BOUNDARIES:

//RIGHT
if (xCord == 4)
{
Console.WriteLine("This area doesn't exist.");
xCord = xCord - 1;
}
//LEFT
if (xCord == -4)
{
Console.WriteLine("This area doesn't exist.");
xCord = xCord + 1;
}
//TOP
if (yCord == 4)
{
Console.WriteLine("This area doesn't exist.");
yCord = yCord - 1;
}
//BOTTOM
if (yCord == -4)
{
Console.WriteLine("This area doesn't exist.");
yCord = yCord + 1;
}
}
}
}
}

这是一个主循环中的大量代码,我建议将功能拆分为更多的函数。

回答你的问题;您的控制台正在发送垃圾邮件的原因";成功了&";。是因为你的";如果";语句位于连续while循环中。您可以尝试在if语句中添加一个额外的布尔值,例如:

//GAMEPLAY
boolean hasMoved = false;
while (true)
{
//2,0
if (xCord == 2 && yCord == 0 && !hasMoved)
{
Console.WriteLine("It worked.");
hasMoved = true;
} 
else{
hasMoved = false;
}

为什么不使用switch case来代替if else呢。此外,您可以在打印it worked后使用break;

相关内容

  • 没有找到相关文章

最新更新