仅陈述一次

问题描述

我正在Visual Studio C#控制台中进行RPG文本冒险。 我正在尝试创建一段代码来检测您的位置。我希望它以必要的格式回复。我知道您可能可以使用数组更有效地执行此操作,但是我对现在的情况感到满意。我想做的是阻止它不断重复显示“它起作用”的信息。如果只声明一次,那么它就起作用了。我尝试了多种方法。请为我的问题提供解决方案。
Code Here or Underneath

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;
                }

            }

        }
    }
}

解决方法

为什么不使用switch case代替if else。 另外,您可以在打印break;

之后使用it worked ,

在一个主循环中有很多代码,我建议将功能拆分为更多功能。

回答您的问题;您的控制台发送垃圾邮件“工作正常”的原因。是因为您的“ if”语句位于连续的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;
    }