我一直试图通过搜索和阅读来整理这一点,而且我知道它必须是简单的东西,但最后一段代码在底部,我正在尝试创建打印输入内容的方法,并且不断收到此错误。无法访问变量 - 超出范围
错误CS0103名称“student1”不会在目前情况下
错误CS0103名称“teacher1”存在没有在当前的背景下 错误CS0103名称“course1”存在没有在当前的背景下
我会永远感激,如果你能指出我正确的方向或至少给我一个提示。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StudentInformation
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("What would you like to do? Please choose a number: ");
Console.WriteLine("");
Console.WriteLine("1. Enter Student Information");
Console.WriteLine("2. Enter Teacher Information");
Console.WriteLine("3. Enter Class Information");
Console.WriteLine();
int menuOne = Convert.ToInt32(Console.ReadLine());
if (menuOne == 1)
{
GetStudent();
}
else if (menuOne == 2)
{
GetTeacher();
}
else if (menuOne == 3)
{
GetClass();
}
else
{
Console.WriteLine("Incorrect input. Exiting application.");
Console.ReadLine();
}
}
public class Student
{
public string fName { get; set; }
public string lName { get; set; }
public string bDate { get; set; }
}
public class Teacher
{
public string tFName { get; set; }
public string tLName { get; set; }
public string tClass { get; set; }
}
public class Course
{
public string cName { get; set; }
public string cSubject { get; set; }
public string cStart { get; set; }
}
static void GetTeacher()
{
Teacher teacher1 = new Teacher();
Console.WriteLine("Enter teacher's first name: ");
teacher1.tFName = Console.ReadLine();
Console.WriteLine("Enter teacher's last name: ");
teacher1.tLName = Console.ReadLine();
Console.WriteLine("Enter teacher's class name: ");
teacher1.tClass = Console.ReadLine();
}
static void GetStudent()
{
Student student1 = new Student();
Console.WriteLine("Enter student's first name: ");
student1.fName = Console.ReadLine();
Console.WriteLine("Enter student's last name: ");
student1.lName = Console.ReadLine();
Console.WriteLine("Enter student's birthdate: ");
student1.bDate = Console.ReadLine();
PrintStudent();
}
static string GetClass()
{
Course course1 = new Course();
Console.WriteLine("Enter class name: ");
course1.cName = Console.ReadLine();
Console.WriteLine("Enter class subject: ");
course1.cSubject = Console.ReadLine();
Console.WriteLine("Enter class start time: ");
course1.cStart = Console.ReadLine();
return course1.cName;
}
static void PrintStudent()//Going to add PrintTeacher and PrintCourses as well after I figure this out
{
//string student1.fName = GetStudent();
// Console.WriteLine(fName);
Console.WriteLine("{0} and {1} are in {2}", student1.fname, teacher1.tFName, course1.cName);
}
}
}
他们是本地变量... –
这段代码需要清理..它的全部联合! –
你应该避免使用诸如'fName'或'cFName'之类的神秘ID。在这两种情况下,一个普通的老名字“FirstName”会更好。 –