C# 如何最好地将数组项的元素与用户输入进行比较
我正在收集3个用户输入,我想在这个数组上运行这些输入,然后返回指定的信息,还有2个用户输入,但我不想阻塞这个问题:C# 如何最好地将数组项的元素与用户输入进行比较,c#,C#,我正在收集3个用户输入,我想在这个数组上运行这些输入,然后返回指定的信息,还有2个用户输入,但我不想阻塞这个问题: //variable to hold the return value from readline - set to 0 int c1 = 0; //if that var is 0 go - do while loop - while var = 0 go if (c1 == 0)
//variable to hold the return value from readline - set to 0
int c1 = 0;
//if that var is 0 go - do while loop - while var = 0 go
if (c1 == 0)
{
while (c1 == 0)
{
Console.WriteLine("Please enter a C# Course ID:\n 10 = Basic C# \n 11 = Advanced C# \n 12 = Complex C# \n Only the course ID is valid as an entry");
int ID;
//Readline - pass variable back for what was entered
bool isNumeric = int.TryParse(Console.ReadLine(), out ID);
//validation check as to what was entered ; i.e. >=10 <=12
if (ID == 10)
{
Console.WriteLine("You have chosen:\n 10 = Basic C#");
c1 = 10;
}
else if (ID == 11)
{
Console.WriteLine("You have chosen:\n 11 = Advanced C#");
c1 = 11;
}
else if (ID == 12)
{
Console.WriteLine("You have chosen:\n 12 = Complex C#");
c1 = 12;
}
//if var outside of range, hard set to 0
else if (ID >= 10 && ID <= 12)
{
Console.WriteLine("Enter a valid input between 10 and 12");
}
}
课程班的设置如下:
public abstract class Courses
{
public DateTime CourseDate { get; set; }
public int StreamID { get; private set; }
public string CourseName { get; private set; }
public int CourseID { get; private set; }
public Courses(int courseID, int streamID, string courseName, DateTime courseDate)
{
this.CourseID = courseID;
this.StreamID = streamID;
this.CourseName = courseName;
this.CourseDate = courseDate;
}
}
主要问题是如何将c1用户输入与数组项中的CourseID第一个数字进行比较。再一次,提前感谢,很抱歉有这么多问题 使用Linq:
var course = courses.SingleOrDefault(x => x.CourseID == c1);
if (course == null){
// No course found
}
课程看起来像一个对象数组,所以为了清晰起见,我们当然需要知道这个类是什么样子的@ŇssaPøngjǣrdenlarp,我已经编辑了课程类结构
var course = courses.SingleOrDefault(x => x.CourseID == c1);
if (course == null){
// No course found
}