[英]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.