【发布时间】:2016-07-04 15:02:36
【问题描述】:
我只是想知道您是否可以快速为我回答问题。我正在做一个文字冒险游戏,我试图让玩家在主区域中设置他们的名字,并将其存储到一个变量中并在其他方法中使用它。
public static void Main(string[] args)
{
//intro
System.Console.WriteLine("Welcome to TextAdventure!");
System.Console.WriteLine("This is an entry level program made by JussaPeak");
System.Console.WriteLine("commands will be noted in the text by (). enter an applicable choice.");
System.Console.WriteLine(" ");
System.Console.WriteLine(" ");
System.Console.WriteLine("Please enter your name:");
string name = Convert.ToString(Console.ReadLine());
System.Console.WriteLine(" ");
System.Console.WriteLine("Hello, {0}. You are about to embark on a lackluster journey of my first text adventure. i hope you enjoy!", name);
System.Console.WriteLine(" ");
System.Console.WriteLine(" ");
System.Console.WriteLine(" ");
System.Console.WriteLine(" ");
System.Console.WriteLine(" ");
StartingArea();
}
public static void GypsyConvo2()
{
Console.WriteLine("You decide to stay and hear her out");
Console.ReadKey();
Console.WriteLine("{0}: Who is this person?", name);
Console.ReadKey();
Console.WriteLine("Gypsy: He is the shapeshifting king from the mountain in the west. His land is untouched by ours.");
Console.WriteLine("'I believe he is plotting to take our land...'");
Console.ReadKey();
Console.WriteLine(" ");
Console.WriteLine("{0}: and what am i to do about that?", name);
Console.ReadKey();
Console.WriteLine("Gypsy: You must warn the queen. i've found an old sewer maintainence that can lead near the entrance to the castle itself. You'll have to find your own way from there.");
}
那么我将如何使从 main 中的 ReadLine 分配值的变量在其他方法中可用而不会引发错误?
【问题讨论】:
-
"不会给我带来错误" -- 什么错误?你试过什么?您无法弄清楚什么错误?当然,在您的静态类中,您可以声明可以由类中所有不同方法共享的静态字段。一种方法可以设置字段,任何其他方法都可以读取字段。完全不清楚您的 question 是什么(正如 FGITW 的答案在试图猜测您的意思时跌倒自己所证明的那样)。
-
我在 Main 中声明了变量。它从 ReadLine 获取值。我试图在其他方法中使用它,但它一直给我一些错误。错误,例如:program.name 的非静态字段、方法或属性需要对象引用。当它没有在 main 之外声明时,它只是告诉我它在当前上下文中不存在。我尝试更改不同方法的返回类型,将变量添加为参数,但它只是不断向我抛出这些错误。
-
我的道歉。感谢您让我知道要搜索特定的错误消息,而不是一般问题。
-
如果您同意您的问题与提议的问题重复,您应该继续投票以关闭您自己的问题作为重复问题,并使用其他问题作为目标。这将确保更容易找到彼此相关的相关问题。
-
如果已回答的问题用作参考形式,则应删除其他问题。它实际上并没有问一个问题,它只是说“我的问题是什么。”,结果和我的一样。为什么我们不保留/优先考虑清楚概述问题并得到答案的问题?我真的在问。
标签: c#