很抱歉发布这个垃圾代码墙:(我不想遗漏任何东西!我还是新人,并且比我自己领先一点。我想把“开关案例”放在一个新课程中,所以我可以更快地引用它,同时也学习制作一个新的类文件。不幸的是我遇到了一个接一个的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();
}
}
}
答案 0 :(得分:4)
看起来您的类位于单独的命名空间中,并且程序的现有命名空间不使用using语句来包含您的单独类。
答案 1 :(得分:1)
如果您使用static
方法/变量,则不应该使用该对象,您应该使用该类本身:
Program.output = "bla";
Program.WriteLine();
而不是
p.output = "bla";
p.WriteLine();
由于您的字段p
甚至没有被初始化,您当前的代码不应该编译。如果你想使用这个对象,你应该这样传递它:
public void Script (int s, Program p)
然后你也可以使你的方法非静态。
另外,我想知道为什么你将光标位置设置为右边文本长度的一半 - 为什么不是完整的偏移?
此外你说的是
我遇到的主要问题是,当我在main方法中调用案例编号时,它只返回字符串。
返回字符串是什么意思?您的Script
方法甚至没有返回类型(void除外)。