调用方法时如何使用局部变量?

时间:2016-10-12 03:07:36

标签: c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MethodsExceptions2
{
    class Program
    {
        static void Main(string[] args)
        {
            GetStudentInformation();
            PrintStudentDetails(firstName, lastName,birthDay);
            Console.ReadKey();
        }

        static void GetStudentInformation()
        {
            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(string first, string last, string birthday)
        {
            Console.WriteLine("{0} {1} was born on: {2}", first, last, birthday);
        }
    }
}

如何在方法调用中输入这些值?当我运行程序时,该行在变量点中显示为空白。我试图用getStudentInfo方法获取用户输入,然后将其存储在变量中,并将其输入printStudentInfo方法进行格式化并将其写入控制台。

3 个答案:

答案 0 :(得分:0)

这段代码根本不应该编译和运行。您在范围内没有firstName,lastName或birthday变量。你用什么编辑器写这个?

如果你想保留变量,那么在方法之外声明它们并以相同的方式分配它们但没有'字符串'修改。像这样......

class Program
{
    static string firstName;
    static string lastName;
    static string birthday;

    static void Main(string[] args)
    {
        GetStudentInformation();
        PrintStudentDetails(firstName, lastName, birthday);
        Console.ReadKey();
    }

    static void GetStudentInformation()
    {
        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);
    }
}

答案 1 :(得分:0)

class Program
{
    static void Main(string[] args)
    {
        var userInputs = GetStudentInformation();
        PrintStudentDetails(userInputs);
        Console.ReadKey();
    }

    static Tuple<string, string, string> GetStudentInformation()
    {
        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();
        return new Tuple<string, string, string>(firstName, lastName, birthDay);
    }

    static void PrintStudentDetails(Tuple<string, string, string> userInputs)
    {
        Console.WriteLine("{0} {1} was born on: {2}", userInputs.Item1, userInputs.Item2, userInputs.Item3);
    }
}

答案 2 :(得分:-1)

进行此更改,您应该能够得到您想要的内容。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MethodsExceptions2
{
  class Program
{
    public static string firstName { get; set; }
    public static string lastName { get; set; }
    public static string birthDay { get; set; }


    static void Main(string[] args)
    {

        GetStudentInformation();
        PrintStudentDetails(firstName, lastName, birthDay);
        Console.ReadKey();
    }

    private static void PrintStudentDetails(string firstName, object lastName, object birthDay)
    {
        Console.WriteLine("{0} {1} was born on: {2}", firstName, lastName, birthDay);
    }

    private static void GetStudentInformation()
    {
        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();

    }



}
}

创建静态属性来保存值并使用它在Main()方法中调用的任何方法。注意在程序类下创建静态属性。从这里创建关于属性的内容C# Properties