将变量传递到c#中的不同方法

本文关键字:方法 变量 | 更新日期: 2023-09-27 17:54:19

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

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.");

}

那么,我该如何使它成为从ReadLine中主要分配值的变量,在其他方法中可用,而不会给我带来错误呢?

将变量传递到c#中的不同方法

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

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

并保持类似CCD_ 4的方法相同。

不是在Main中声明名称变量,而是在类成员级别(out-side 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
}