c# – 第一次为方法使用单独的类(.cs)文件

很抱歉发布这个垃圾代码墙:(我不想留下任何东西!我还是新的,并且比我自己先前一点.我想把“switch case”放在一个新的类中,这样我就可以更快地引用它同时也学习制作一个新的类文件.不幸的是我遇到了一个接一个的bug,我无法弄清楚原因.我遇到的主要问题是,当我在main方法中调用案例编号时,它只返回字符串.

防爆.我希望输出为:
 “那么[用户创建的名字],你准备好开始你的世界边缘之旅吗?”

似乎两个类相互通信存在问题.
我更糟糕的是在所有事情上抛出“公共静态”以试图让它发挥作用.我真的很感激帮助.

using System;
using Test;

public class Scripts
{
    public Program p { get; set; }

    public void Script(int s)
    {
        switch (s)
        {
            case 1:
                p.output = "Quest to the edge of the world!";
                p.WriteLine();
                break;
            case 2:
                p.output = "Hello! I am your instructor. What is your name apprentice?";
                p.WriteLine();
                break;
            case :
                p.output = p.name + " you say? My, what a strange name.";
                p.WriteLine();
                break;
            case 4:
                p.output = "Well " + p.name + ", are you ready to begin your journey to the edge of the world?";
                p.WriteLine();
                break;
            case 5:
                p.output = " (y)Yes on (n)No";
                p.WriteLine();
                break;
            case 6:
                p.output = "OK! Before we go lets go over our gear. What should we prioritize? (1)Magic, (2)Melee, or (3)Ranged?";
                p.WriteLine();
                break;
            case 7:
                p.output = "Well I'll just come back tomorrow then.";
                p.WriteLine();
                break;
            case 8:
                p.output = "I'm sorry... that's not an option.";
                p.WriteLine();
                break;
            case 9:
                p.output = "I'm pretty good at " + p.combatStyle + " combat myself, I'll be sure to teach you everything I know.";
                p.WriteLine();
                break;
            case 10:
                p.output = "OK! Let's hit the road and make camp at sundown.";
                p.WriteLine();
                break;
            case 11:
                p.output = "Chapter " + p.chapter;
                p.WriteLine();
                break;
            case 12:
                p.output = p.name + ", wake up!Were under attack by a couple of goblins!";
                p.WriteLine();
                break;
            case 13:
                p.output = "I guess this is the perfect chance to teach you a new" + p.form + "called";
                p.WriteLine();
                break;
            case 14:
                p.output = "It makes quick work of their health, but your" + p.energyBar + " will go down just as fast so use it wisely!";
                p.WriteLine();
                break;
            case 15:
                p.output = "I'll take care of the one on the right, you engage the one on the left.";
                p.WriteLine();
                break;
            case 16:
                p.output = "Press (f) to enter combat. Then press (1) to make an attack";
                p.WriteLine();
                break;

            default:
                break;
        }
    }

}

using System;
using System.Threading;

namespace Test
{
    public class Program
    {
        public Scripts s { get; set; }
        // Class Global Variables
        public static string input;
        public static string output;
        public static string name = "bob";
        private static string yes = "y";
        private static string no = "n";
        private static string option1 = "1";
        private static string option2 = "2";
        private static string option3 = "3";
        private static string pathA;
        private static string pathB;
        private static string pathC;
        public static string combatStyle;
        private static string weapon;
        public static string chapter = "Zero";
        public static string form;
        public static string energyBar;
        private static string healthBar;
        Program p = new Program();
        public static string[] numbers = new string[] { "One","Two", "Three", "Four", "Five" };
        private static int mp = 100;
        private static int stamina = 100;
        private static int script;



        //ReadLine & WriteLine Methods
        private static void ReadLine()
        {
            Console.SetCursorPosition((Console.WindowWidth - 110) / 2, Console.CursorTop);
            input = Console.ReadLine();
        }
        public static void WriteLine()
        {
            Thread.Sleep(2000);
            Console.SetCursorPosition((Console.WindowWidth - output.Length) / 2, Console.CursorTop);
            Console.WriteLine(output);
        }

        //Next Chapter Screen
        private static void nextChapter()
        {
            Thread.Sleep(3000);
            Console.Clear();
            Thread.Sleep(1500);
            output = "\r\n\r\n\r\n\r\n";
            WriteLine();
            //output = script[12] + chapter;
            WriteLine();
            Console.Beep();
            Thread.Sleep(1500);
            Console.Clear();



        }

        //Yes or No Decision
        private static void YesNo()
        {
            while (input != yes && input != no)
            {
                ReadLine();
                if (input == yes)
                {
                    output = pathA;
                    WriteLine();
                }
                else if (input == no)
                {
                    output = pathB;
                    WriteLine();
                }
                else
                {
                    //output = script[8];
                    WriteLine();
                }
            }
            input = "";
        }

        //Multiple Choice Decision
        private static void Choice()
        {
            while (input != option1 && input != option2 && input != option3)
            {
                ReadLine();
                if (input == option1)
                {
                    output = pathA;
                    WriteLine();
                }
                else if (input == option2)
                {
                    output = pathB;
                    WriteLine();
                }
                else if (input == option3)
                {
                    output = pathC;
                    WriteLine();
                }
                else
                {
                    //output = //script[8];
                    //WriteLine();
                }
            }
            input = "";
        }

        //Combat Loop
        private static void combatLoop()
        {
            if (input == "f")
            {

            }
            else
            {
                //output = script[8];
                WriteLine();
                combatLoop();
            }
        }

        //Main Method
        static void Main()
        {


            //Chapter Zero "Intro"
            Scripts s = new Scripts();
            s.Script(1);
            s.Script(2);
            ReadLine();
            name = input;
            Console.WriteLine(input);
            Console.WriteLine(name);
            s.Script(3);

            //Chapter One "Combat"
            //Chapter Two "Trading"
            //Chapter Three "Dungeon"
            //Chapter Four "Sailing"
            //Chapter Five "The Edge"
            // Keep the console open in debug mode.
            Console.WriteLine("Press any key to quit game.");
            Console.ReadKey();
        }
    }
}
最佳答案
看起来您的类位于单独的命名空间中,并且程序的现有命名空间不使用using语句来包含您的单独类.

转载注明原文:c# – 第一次为方法使用单独的类(.cs)文件 - 代码日志