繁体   English   中英

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

[英]How do I use local variables when calling a method?

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方法中以对其进行格式设置并将其写入控制台。

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

如果要保留变量,则在方法之外声明它们,并以相同的方式分配它们,但不带'string'修饰符。 像这样...

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

进行此更改,您应该能够获得所需的内容。

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

    }



}
}

静态属性程序class.Read下创建距离这里大约属性创建静态属性来保存的价值和使用它,你在main()method.Note呼吁任何方法C#属性

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM