【问题标题】:How can I call a method within a method and use the same variables如何在方法中调用方法并使用相同的变量
【发布时间】:2015-04-16 14:40:15
【问题描述】:

我是编程新手,但遇到了 C# 问题。我想创建一个控制台程序,用户填写一些个人信息,控制台打印这些信息。我正在尝试使用 ref,但我无法将用户的答案(来自方法 GetStudentInfo)与打印方法 PrintStudentDetails 联系起来。

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            GetStudentInfo();
            {
                string first = "";
                string last = "";
                string birthday = "";
                PrintStudentDetails(ref first, ref last, ref birthday);
            }
            GetTeacherInfo();
            GetCourseInfo();
            GetProgramInfo();
            GetDegreeInfo();
        }
        //student information
        static void GetStudentInfo()
        {
            Console.WriteLine("Enter the student's first name: ");
            string firstName = Console.ReadLine();
            Console.WriteLine("Enter the student's last name: ");
            string lastName = Console.ReadLine();
            Console.WriteLine("Enter the student's birthday: ");
            string birthDay = Console.ReadLine();
        }
        static void PrintStudentDetails(ref string first, ref string last, ref string birthday)
        {
            first = "test";
            last = "test";
            birthday = "test";
            Console.WriteLine("{0} {1} was born on: {2}", first, last, birthday);
            Console.ReadLine();
        }
    }
}

【问题讨论】:

  • 这段代码甚至无法编译。
  • 只返回一个带有名字/生日的小类?
  • 您具体遇到了什么错误?
  • 我要控制台写:
  • 我想让控制台写 Enter the student's first name:..i.e."A"., , 然后 Enter the student last name..."B".. , 输入学生的生日.. “C”..然后打印用户填写的内容。例如:“A B出生于:C”但我无法连接这些变量。现在我得到“测试测试哇出生于:测试”。 (我用这个来测试它)

标签: c# console ref


【解决方案1】:

您需要将字符串 refs 传递给 GetStudentInfo,我认为最好使用 out 而不是 ref。
Out 与 ref 相同,只是在方法返回之前 out 参数必须有一个值。

static void Main(string[] args)
{
    string first;
    string last;
    string birthday;
    GetStudentInfo(out first,out last,out birthday); 
    PrintStudentDetails (first, last, birthday);    
    GetTeacherInfo();
    GetCourseInfo();
    GetProgramInfo();
    GetDegreeInfo();
}
static void GetStudentInfo(out string firstName ,out string lastName,out string birthDay)
{
    Console.WriteLine("Enter the student's first name: ");
    firstName = Console.ReadLine();
    Console.WriteLine("Enter the student's last name: ");
    lastName = Console.ReadLine();
    Console.WriteLine("Enter the student's birthday: ");
    birthDay = Console.ReadLine(); 
}
static void PrintStudentDetails(string first, string last, string birthday)
{
    Console.WriteLine("{0} {1} was born on: {2}", first, last, birthday);
    Console.ReadLine();
}

https://msdn.microsoft.com/en-us/library/t3c3bfhx.aspxhttps://msdn.microsoft.com/en-us/library/t3c3bfhx.aspx

【讨论】:

    【解决方案2】:
    namespace ConsoleApplication3
    {
        class Program
        {
            static void Main(string[] args)
            {
                //variables before the method that needs them, not after
                string first ="";
                string last ="";
                string birthday ="";
                GetStudentInfo(ref first, ref last, ref birthday);
                //removed extra brackets
                PrintStudentDetails(first, last, birthday);
                GetTeacherInfo();
                GetCourseInfo();
                GetProgramInfo();
                GetDegreeInfo();
            }
    //student information
            //passed references in to this method
            static void GetStudentInfo(ref string firstName, ref string lastName, ref string birthday)
            {
                Console.WriteLine("Enter the student's first name: ");
                firstName = Console.ReadLine();
                Console.WriteLine("Enter the student's last name: ");
                lastName = Console.ReadLine();
                Console.WriteLine("Enter the student's birthday: ");
                birthday = Console.ReadLine(); 
            }
            static void PrintStudentDetails(string first, string last, string birthday)
            {
                //removed lines that reassigned variables
                Console.WriteLine("{0} {1} was born on: {2}", first, last, birthday);
                Console.ReadLine();
            }
        }
    }
    

    你最大的问题是语法,它不能像写的那样工作。 Ref 将变量地址传递给方法,基本上你需要先分配变量,然后才能传递它,一旦传递它,方法内部变量发生的任何事情也会发生在它外部,如果这有意义的话。

    如果你想在 get info 方法中调用 print 方法

     PrintStudentDetails(first, last, birthday);
    

    里面

    GetStudentInfo(ref string firstName, ref string lastName, ref string birthday)
    {
        //move here and change the variables too firstName, lastName, birthday, instead of first, last, birthday    
    }
    

    【讨论】:

    • 做功课连个大拇指都没有=(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 2016-08-27
    • 2019-12-23
    • 2023-03-19
    • 2019-09-09
    • 2011-05-07
    相关资源
    最近更新 更多