using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Methods
{
class Program
{
static string firstName;
static string lastName;
static string birthday;
static void Main(string[] args)
{
GetStudentInformation();
//PrintStudentDetails(firstName, lastName,birthDay);
Console.WriteLine("{0} {1} {2}", 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();
//Console.WriteLine("{0} {1} {2}", firstName, lastName, birthDay);
}
static void PrintStudentDetails(string first, string last, string birthday)
{
Console.WriteLine("{0} {1} was born on: {2}", first, last, birthday);
}
}
}
我已经尝试了各种方法建议我关于如何声明类变量,但我得到的每个解决方案似乎都不起作用。我试图将来自用户的输入保存为3个变量;姓氏,名字和生日。每当你运行程序时,它都会询问这些值,当它试图打印变量时,它只显示一个空行。我怎样才能得到一个方法调用工作类变量
如何以这种方式输出我的变量?
您将重新声明打印中使用的三个全局变量。这隐藏了全局变量和输入发生在代码从GetStudentInformation退出后立即丢弃的三个局部变量 – Steve