使用用户输入填充对象数组。 C#

时间:2012-12-07 06:44:12

标签: c# arrays object user-input

我试图用C#完成这两个目标。我错过了解释如何用对象填充数组的类,所以我真的没有一个起点如何做第二个目标。

“1。创建一个包含两个字段Student Class(String)和StudentNumber(int)的Student类,相应的属性(即get和set)和构造函数(默认和非默认)。学生名称默认为空字符串,而学号将默认为-1。

  1. 使用Student类解决以下问题。 创建一个程序,允许用户将最多24名学生输入一个阵列,确保学生编号正好是5位数。输入所有学生后,允许用户按名称或编号对学生进行连续搜索。如果找到,则显示完整的学生姓名和号码,否则显示错误消息。允许用户继续搜索,直到他们决定退出。“
  2. 第一部分我上班了,这是我的学生班:

    class Student
    {
        // fields
        private string _studentName;
        private int _studentNumber;
    
        // properties
        public string studentName
        {
            get
            {
                return _studentName;
            }
            set
            {
                studentName = value;
            }
        }
        public int studentNumber
        {
            get
            {
                return _studentNumber;
            }
            set
            {
                studentNumber = value;
            }
        }
        // constructors
            // default - no parameters
        public Student()
        {
            _studentName = "";
            _studentNumber = -1;
        }
            // non default - takes perameters
        public Student(string studentName, int studentNumber)
        {
            _studentName = studentName;
            _studentNumber = studentNumber;
        }
    }
    

    这是我的主要计划:

    class Program
    {
        static void Main(string[] args)
        {
            ////////////// Question 1 ////////////////
    
            // create new student
            Student defaultStudent = new Student();
            // display student
            InputOutput.DisplayStudentInformation(defaultStudent);
            // keep console open
            Console.ReadLine();
        }
    }
    

    现在我遇到了目标第二部分的问题。我不知道如何使用Object(student)类来帮助我创建一个用户输入的数组,因为我错过了那个特定的讲座。

    我不是要求有人为我完成整个作业,我只是不知道如何使用带有用户输入的studentName和studentNumber填充数组。

    我最终试图在这里找到一个起点。任何人吗?

3 个答案:

答案 0 :(得分:1)

使用Console.ReadLine()从控制台应用程序获取输入。请参阅:http://msdn.microsoft.com/en-us/library/system.console.readline.aspx

获得名称和编号并执行验证后,创建学生对象并添加到数组

List<Student> students = new List<Student>();
            Console.WriteLine("Enter name:");
            string nameInput = Console.ReadLine();
            // alternative is to generate own student number
            Console.WriteLine("Enter number:");
            string numberInput = Console.ReadLine();
            // perform validations then create Student                        
            int number;
            // check result of TryParse
            int.TryParse(numberInput, out number);            
            students.Add(new Student { Number = number, Name = nameInput });

答案 1 :(得分:0)

在main方法中,您可以创建Student Object数组。

Student[] ourStudents = new Student[24];   // declared an array of stduent objects

您可以使用For循环进行迭代,以使用可能包含学号和姓名的其他数组填充每个学生对象。否则,您可以将这些对象绑定到控制台输入/表单文本框:)

手动填写每个对象:

ourStduents[0].Number = 12345;
ourStudents[0].Name = "John Kent";

当你在控制台窗口中完成所有工作时,请忽略Form的部分。

for (int i = 0; i<24; i++) //just user the static array length
{
      myStudents[i].Number = Console.ReadLine();
      myStudents[i].Name = Console.ReadLine();
}

添加更多代码以进一步解释OP的评论

假设所有学生都能很好地学习,main课程就是这样。

using System;
namespace array_sample
{
    class StudentData
    {
        static void Main(string[] args)
        {
            Regex num = new Regex(@"^\d{5}$");
            Student[] ourStudents = new Student[24];   
            // declared an array of stduent objects 

            for (int i = 0; i < 24; i++)
            {
                Console.WriteLine("Enter your Student Number :");
                Match n = num.Match(Console.ReadLine());
                if (n.Value != "")
                {
                  ourStudents[i].Number = Int32.Parse(Console.ReadLine()); 
                  // make sure to convert to integer

                  Console.WriteLine("Enter your Name :");
                  ourStudents[i].Name = Console.ReadLine(); 
                }
                Else
                {
                  Console.WriteLine("Number Can only be 5 digits");
                  if (i > 0)
                     {i = i - 1;}
                  else
                     {i = 0;}
                }

            } // end of input loop

            for(i=0; i<24; i++)
            {
                Console.WriteLine("Number : " + ourStudents[i].Number +'\t' + "Name :"
                                  + ourStudents[i].Name);
            }// end of output loop

            Console.ReadLine();
        }
    }// end of class
} // end of namespace

答案 2 :(得分:-1)

Import TerminalIO.*;
Import BreezySwing.*;

Insert Codes here 
Insert Hacks here
Insert JavaScript here
int CR;
CR=reader.readln("Import TerminalIO")
System.out.println(" +CR+ ")
相关问题