我正在研究考试应用程序(c#console应用程序)
应用程序要求用户输入其名称,我希望应用程序读取该用户的名称和 根据我存储在对象中的详细信息打印用户的详细信息
例如: 如果用户的名称与此对象中的名称匹配:
students s3 = new students("Dee", "Scott", "Computing", 100m, 66.6m);
如何打印此用户的详细信息。
我有一个单独的方法打印出用户的详细信息
public string gradeDetails {
get { return FirstName + LastName + Course + FinalGrade; }
}
我无法弄清楚如何将用户输入与相应的对象进行匹配。
答案 0 :(得分:0)
您可以使用Console.Readline()方法
public static void Main()
{
string line;
Console.WriteLine("Enter one or more lines of text (press CTRL+Z to exit):");
Console.WriteLine();
do {
Console.Write(" ");
line = Console.ReadLine();
if (line != null)
Console.WriteLine(" " + line);
} while (line != null);
答案 1 :(得分:0)
Console.ReadLine();
Console.WriteLine(); or Console.Write();
我不确定这是不是你要问的,但那些是读写的电话。
这个信息很容易谷歌。
答案 2 :(得分:0)
以下示例将执行您的要求。如果您对我使用的代码有任何疑问,请随时提出。
class Student
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Course { get; set; }
public decimal FinalGrade { get; set; }
public Student(string firstName,
string lastName,
string course)
{
FirstName = firstName;
LastName = lastName;
Course = course;
FinalGrade = 0;
}
// This will first call the constructor above and then continue.
public Student(string firstName,
string lastName,
string course,
decimal finalGrade) : this(firstName, lastName, course)
{
FinalGrade = finalGrade;
}
// By overriding ToString we can use Console.WriteLine(student) directly.
public override string ToString()
{
return string.Format(@"FirstName: {0}, LastName: {1}, Course: {2}, FinalGrade: {3}",
FirstName,
LastName,
Course,
FinalGrade);
}
}
class Program
{
static void Main(string[] args)
{
// Create our students.
List<Student> students = new List<Student>
{
new Student("John", "Test", "Computing"),
new Student("Tim", "Test", "Computing", 8.25m)
};
string user = "";
do
{
Console.Clear();
Console.WriteLine("Please enter the name of the student:");
user = Console.ReadLine();
if (user.Equals("exit", StringComparison.OrdinalIgnoreCase))
break;
// Find the student or return null.
Student student = students.FirstOrDefault(s => s.FirstName.Equals(user, StringComparison.OrdinalIgnoreCase));
if (student != null)
{
Console.WriteLine("Student info:");
Console.WriteLine(student);
}
else
{
Console.WriteLine("Student '" + user + "' not found.");
}
Console.WriteLine();
// Wait until a key is pressed.
Console.WriteLine("Press a key to continue..");
Console.ReadKey(true);
} while (true);
}
}
答案 3 :(得分:0)
做这样的事情: 创建一个名为Student的课程:
public class Student
{
public string Fname { get; set; }
public string LName { get; set; }
public string Course { get; set; }
public string FinalGrade { get; set; }
}
然后这样做
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter you name:");
string name = Console.ReadLine();
Console.WriteLine(gradeDetails(name));
Console.ReadLine();
}
public static string gradeDetails(string name)
{
List<Student> students = new List<Student>()
{
new Student{ Fname = "Scott",LName ="Dee",Course = "Computing", FinalGrade = "66.66m"},
new Student{Fname = "Joe",LName = "Don",Course = "Chemestry", FinalGrade = "80.77m"}
};
var student = students.SingleOrDefault(s => s.LName.ToLower() == name.ToLower());
if (student!=null)
{
return student.Fname + "" + student.LName + "" + student.Course + "" + student.FinalGrade;
}
return string.Empty;
}
}
答案 4 :(得分:0)
询问用户输入名称和打印名称
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace stackoverflow1
{
class Program
{
static void Main(string[] args)
{
string name;
Console.WriteLine("Enter your name : ");
name = Console.ReadLine();
Console.WriteLine("Hello " + name + " , Welcome to OOP!");
Console.ReadKey();
}
}
}