方法}预期
本文关键字:预期 方法 | 更新日期: 2023-09-27 18:04:02
得到以下代码,第一个错误是'}expected'
正在尝试获取和打印学生数据。不确定是否需要让这些方法成为返回类型之类的。欢迎提出任何建议,谢谢。
namespace studentInfo
{
class Program
{
static void Main(string[] args)
{
getUserInformation();
printStudentDetails(string firstName, string lastName, string birthday);
}
static void getUserInformation()
{
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 your bithdate");
//DateTime birthdate = Convert.ToDateTime(Console.ReadLine());
string birthday = Console.ReadLine();
}
static void printStudentDetails(string firstName, string lastName, string birthday)
{
Console.WriteLine("{0} {1} was born on: {2}", firstName, lastName, birthday);
Console.ReadLine();
}
}
}
你正在错误地调用一个方法:
printStudentDetails(string firstName, string lastName, string birthday);
应: printStudentDetails(firstName, lastName, birthday);
您还必须在将变量firstName,lastName,birthday传递给您的方法之前定义它们。
你做错了!
你只需要变量来调用方法,你不需要它的数据类型。所以应该是这样的:-
printStudentDetails(firstName, lastName, birthday);
,如果你想使用这样的变量,那么全局声明它们,如:-
public static dynamic firstName;
public static dynamic lastName;
public static dynamic birthday;
static void Main(string[] args)
{
getUserInformation();
printStudentDetails(firstName, lastName, birthday);
}
static void getUserInformation()
{
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 your bithdate");
birthday = Console.ReadLine();
}
static void printStudentDetails(string firstName, string lastName, string birthday)
{
Console.WriteLine("{0} {1} was born on: {2}", firstName, lastName, birthday);
Console.ReadLine();
}