将变量传递给c#

时间:2016-07-04 05:54:12

标签: c#

我只是想知道你是否可以快速回答我的问题。我正在做一个文本冒险游戏,我试图让玩家在主区域设置他们的名字,并将其存储到变量并在其他方法中使用它。

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值的变量,在其他方法中可用而不会给我带来错误?

2 个答案:

答案 0 :(得分:4)

如果要将变量传递给方法,则需要更改方法的签名以接受参数:

public static void GypsyConvo2(string name)
{

}

当你调用方法时,你就像这样传递name

GypsyConvo2(name);

方法GypsyConvo2中的用法:

Console.WriteLine("{0}: Who is this person?", name);

表格C#规格:

  

1.6.6.1参数

     

参数用于将值或变量引用传递给方法。   方法的参数从参数中获取实际值   调用方法时指定的。有四种   参数:值参数,参考参数,输出参数,   和参数数组。

或者如果您不想传递它:

static string name;
static 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:");
    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();
}

并保持GypsyConvo2之类的方法相同。

答案 1 :(得分:3)

不是在Main中声明名称变量,而是在类成员级别(out main)中声明一个私有变量。这就是你如何在方法之间共享变量值。

private string Name;
public static void Main(string[] args)
{
    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:");
        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();
}

其他方法是在方法中使用参数,但你拥有的变量名是Name,它听起来是一个类成员,这不符合这里。但是如果你觉得它不是一个班级成员,或者你有其他情况,你觉得它不是班级,那么你可以在GypsyConvo2()中声明一个参数

public static void GypsyConvo2(string name)
{
     //your code goes here
}