-3
class Program
{
    public static void GetSchoolInfo()
    {
        string SchoolName,
               EnrollmentStatus,
               ColorOne,
               ColorTwo;

        Console.WriteLine("Please enter your school name: ");
        SchoolName = Console.ReadLine();
        Console.WriteLine("Please enter your Enrollment Status: ");
        EnrollmentStatus = Console.ReadLine();
        Console.WriteLine("Pleas enter one of your school's colors: ");
        ColorOne = Console.ReadLine();
        Console.WriteLine("Please enter the other school color: ");
        ColorTwo = Console.ReadLine();
    }
    static void Main(string[] args)
    {
        GetSchoolInfo();

        Console.ReadLine();

    }

ETC

我的目的是创建一个方法来获取这些字符串,然后很好地输出它们。在方法之外,我似乎无法调用 SchoolName 字符串或任何其他字符串。我目前正在为我的 CS 巴赫上学,所以请原谅我,因为这是一个新手问题。

4

6 回答 6

2

不是返回 void,而是返回一个可以在 main 中使用的类的新实例:

        public class SchoolInfo
        {
            public string SchoolName { get; set; }
            public string EnrollmentStatus { get; set; }
            public string ColorOne { get; set; }
            public string ColorTwo { get; set; }
        }

        public static SchoolInfo GetSchoolInfo()
        {
            string SchoolName,
                   EnrollmentStatus,
                   ColorOne,
                   ColorTwo;

            Console.WriteLine("Please enter your school name: ");
            SchoolName = Console.ReadLine();
            Console.WriteLine("Please enter your Enrollment Status: ");
            EnrollmentStatus = Console.ReadLine();
            Console.WriteLine("Pleas enter one of your school's colors: ");
            ColorOne = Console.ReadLine();
            Console.WriteLine("Please enter the other school color: ");
            ColorTwo = Console.ReadLine();

            return new SchoolInfo()
                {
                    SchoolName = SchoolName,
                    EnrollmentStatus = EnrollmentStatus,
                    ColorOne = ColorOne,
                    ColorTwo = ColorTwo
                };
        }


        private static void Main(string[] args)
        {
            SchoolInfo info = GetSchoolInfo();
            Console.WriteLine("You Entered School Name: "+info.SchoolName);
            Console.ReadLine();

        }
于 2013-02-05T00:51:41.113 回答
1

这是因为它们的范围。范围由定义变量的位置确定,它们是在方法内部的示例中定义的。这就是他们的范围所在。方法很快就会超出范围,因此,它们的局部变量也是如此。如果你想在类之外访问这些变量,你应该将它们定义为类变量,如下所示:

class Program
{
    string SchoolName,
           EnrollmentStatus,
           ColorOne,
           ColorTwo;
public static void GetSchoolInfo()
{

    Console.WriteLine("Please enter your school name: ");
    SchoolName = Console.ReadLine();
    Console.WriteLine("Please enter your Enrollment Status: ");
    EnrollmentStatus = Console.ReadLine();
    Console.WriteLine("Pleas enter one of your school's colors: ");
    ColorOne = Console.ReadLine();
    Console.WriteLine("Please enter the other school color: ");
    ColorTwo = Console.ReadLine();
}
static void Main(string[] args)
{
    GetSchoolInfo();

    Console.ReadLine();

}
于 2013-02-05T00:47:52.667 回答
1

嗨,正如@Adam Maras 提到的,您正在尝试访问一个不再在您的范围内的变量。当您在函数中声明变量时,它会在该函数中生存和消亡。这意味着您将无法再从函数外部访问它。

现在解决方案;创建一个类来保存变量并在您的主应用程序中使用。

下面是一个演示。

这些是编程的基础知识,了解基础知识将带您走很长的路......祝你好运

public class SchoolInfo
{
    /* two properties are defined as public to carry the data */
    public string NameOfTheSchool { get; set; }
    public string AddressOfTheAchool { get; set; }
}


class Program
{
    public static void GetSchoolInfo(SchoolInfo info)
    {
        info.NameOfTheSchool = "AA";
        info.AddressOfTheAchool = "BB";
    }

    static void Main()
    {
        // create the instance of the class
        SchoolInfo mySchoolInfo = new SchoolInfo();

        // pass it into GetSchoolInfo to collect the data
        GetSchoolInfo(mySchoolInfo);

        // print the value of the name of the school
        Console.WriteLine(mySchoolInfo.NameOfTheSchool);

        Console.ReadKey();
    }
}
于 2013-02-05T00:59:53.907 回答
0

您无法在GetSchoolInfo方法之外访问这些变量的原因是它们是局部变量,因此超出了该方法之外的任何范围。我已经阅读了这个主题。

于 2013-02-05T00:47:17.990 回答
0

该方法被声明为一个void函数。因为变量SchoolNameEnrollmentStatusColorOneColorTwo是局部变量并且不返回,所以您不能在GetSchoolInfo()函数之外访问它们。您可以将它们声明为全局变量,也可以创建 aCollection并从函数中返回它们。我更喜欢选项 2,但这取决于你。

于 2013-02-05T00:48:16.523 回答
0

您在 GetSchoolnfo() 方法中定义的变量是该方法的本地变量,这意味着它们只能在该方法中访问。您可以将它们定义为类级别变量而不是方法,以便其他方法也可以访问它们。

class Program
{
    private String SchoolName;
    private String EnrollmentStatus;
    private String ColorOne;
    private String ColorTwo;

    public static void GetSchoolInfo()
    {

        Console.WriteLine("Please enter your school name: ");
        SchoolName = Console.ReadLine();
        Console.WriteLine("Please enter your Enrollment Status: ");
        EnrollmentStatus = Console.ReadLine();
        Console.WriteLine("Pleas enter one of your school's colors: ");
        ColorOne = Console.ReadLine();
        Console.WriteLine("Please enter the other school color: ");
        ColorTwo = Console.ReadLine();
    }
    static void Main(string[] args)
    {
        GetSchoolInfo();

        Console.ReadLine();

    }
于 2013-02-05T00:48:24.417 回答