我正在使用类来获取用户的输入并显示它。请帮助更好地理解课程。(I am using classes to get input from the user and the displaying it. Please assist in getting better understanding of classes.)

86 IT屋

private string numberOfStudent;
private string nameOfStudent;
private int examMarkOfStudent;

public void setNumberOfStudent(string numberOfStudent)
{
   this.numberOfStudent = numberOfStudent;
}

public string getNumberOfStudent()
{
   return this.numberOfStudent;
}

public void setNameOfStudent(string nameOfStudent)
{
   this.nameOfStudent = nameOfStudent;
}

public string getNameOfStudent()
{
   return this.nameOfStudent;
}

public void setExamMarkOfStudent(int examMarkOfStudent)
{
   this.examMarkOfStudent = examMarkOfStudent;
}

public int GetExamMarkOfStudent(int examMarkOfStudent)
{
   return this.examMarkOfStudent;
}

class MyStudent
{
   private Student myStud;

   public MyStudent()
   {
      myStud = new Student();
   }

   public void GetStudentDetails(string numberOfStudent)
   {
      int numberOfStudents;
      double classAverage = 0;
      int lowest = 99, highest = 0, sum = 0;
      int count = 0;
      string studentLowest, studentHighest;

      Console.Write("How many student would you like to add?: ");
      numberOfStudents = int.Parse(Console.ReadLine());

      myStud.setNumberOfStudent(numberOfStudent);

   }

   public void ListStudentDetails()
   {
      Console.WriteLine("Student name: {0}", myStud.getNameOfStudent());
   }
解决方案
Your code has too big problems to discuss anything seriously. Please see my comments to the question. It looks like you really don't have a clue on classes. It's fine, but you need to get started by yourself: read at least one manual on C# and .NET from the very beginning to the very end, doing some simple exercises as you go.

First thing to learn would be the types and the instances. By some reason, you try to add an instance inside a constructor, which would call the same constructor. The instance is already created. Inside methods (including property accessors), the instance is passed as "this". And so on. It's too long story to explain everything. One can help with "better understanding" only if there is at least some understanding to start with. Don't waste time, start reading the manual from the very beginning.

—SA

First off, you'd be better off using properties instead of separate methods - that way your entire top code fragment could be condensed to:
public int NumberOfStudent { get; set; }
public string NameOfStudent { get; set; }
public int ExamMarkOfStudent {get; set; }

And the code that uses it becomes cleaner as well:
Console.Write("How many student would you like to add?: ");
myStud.NumberOfStudents = int.Parse(Console.ReadLine());
But even there, you should be using TryParse instead - that way, you can report an error if the user makes a mistake:
while (true)
    {
    Console.Write("How many student would you like to add?: ");
    int value;
    if (int.TryParse(Console.ReadLine(), out value))
        {
        myStud.NumberOfStudent = value;
        break;
        }
    Console.WriteLine("Numeric value only, please!");
    }



"I want to calculate students average and display list of all student names,marks,minimum and maximum mark mark for those students"

For that, you need to keep a collection of Student or MyStudent objects somewhere - are you familiar with Arrays, or the Generic List<T> collection?

[edit]Forgot a semicolon![/edit]

"Yes thanks I am familiar with arrays but not generic list collection. Only started using classes a few days ago. The lecturers at uni don't make it any easier to understand either"


OK - Arrays are easy, but are limited because you need to say in advance how many elements they will hold. A generic List is better, because you can add and remove elements "on the fly" - but if you haven't met them yet, I'll ignore them as I don't want to introduce stuff too early and confuse the issue.
So we'll assume a largest number of students as 100 - because nobody wants to type that many student details in anyway - and in your main code you can create an array:

MyStudent[] students = new MyStudent[100];


Then in a loop create an instance of the student for each actual student you need to include and fill in it's details:

int nextStudent = 0;
while(moreStudentsToEnter)
    {
    MyStudent student = new MyStudent();
    student.GetStudentDetails();
    students[nextStudent++] = student;
    ...
    }


Then after the the students are all added, you can loop through the students and list them, average their marks, whatever you need to do:

for(int i = 0; i < nextStudent; i++)
    {
    Console.WriteLine(students[i].NameOfStudent);
    }


Get the idea?

  private   string  numberOfStudent; 
private string nameOfStudent;
private int examMarkOfStudent;

public void setNumberOfStudent( string numberOfStudent)
{
this .numberOfStudent = numberOfStudent;
}

public string getNumberOfStudent()
{
return this .numberOfStudent;
}

public void setNameOfStudent( string nameOfStudent)
{
this .nameOfStudent = nameOfStudent;
}

public string getNameOfStudent()
{
return .nameOfStudent;
}

public void setExamMarkOfStudent( int examMarkOfStudent)
{
this .examMarkOfStudent = examMarkOfStudent;
}

public int GetExamMarkOfStudent( int examMarkOfStudent)
{
return this .examMarkOfStudent;
}


  class  MyStudent 
{
私人学生myStud;

public MyStudent()
{
myStud = new 学生();
}

public void GetStudentDetails( string numberOfStudent)
{
int numberOfStudents;
double classAverage = 0 ;
int 最低= 99 ,最高= 0 ,sum = 0 ;
int count = 0 ;
string studentLowest,studentHighest;

Console.Write( 你想添加多少学生?:);
numberOfStudents = int .Parse(Console.ReadLine());

myStud.setNumberOfStudent(numberOfStudent);

}

public void ListStudentDetails()
{
Console.WriteLine( 学生姓名:{0}, myStud.getNameOfStudent());
}
解决方案
您的代码有太大的问题无法认真讨论。请看我对这个问题的评论。看起来你真的没有关于课程的线索。这很好,但你需要自己开始:从头到尾阅读至少一本关于C#和.NET的手册,随时做一些简单的练习。



首先要学习的是类型和实例。出于某种原因,您尝试在构造函数中添加一个实例,该实例将调用相同的构造函数。该实例已创建。在内部方法(包括属性访问器)中,实例作为this传递。等等。解释一切都是太长的故事了。只有在开始时至少有一些理解时,才能帮助更好地理解。不要浪费时间,从一开始就阅读手册。



-SA

首先,你最好使用属性而不是单独的方法 - 这样你的整个顶级代码片段就可以压缩到:

  public   int  NumberOfStudent { get ;  set ; } 
public string NameOfStudent { get ; set ; }
public int ExamMarkOfStudent { get ; set ; }


使用它的代码也变得更干净了:

 Console.Write( 您想添加多少学生?:); 
myStud.NumberOfStudents = int .Parse(Console.ReadLine());
但即便如此,你应该使用TryParse代替 - 这样,如果用户犯了错误,您可以报告错误:

  while  true 
{
Console.Write( 你想加几个学生?:);
int value ;
if int .TryParse(Console.ReadLine(), out value ))
{
myStud.NumberOfStudent = value ;
break ;
}
Console.WriteLine( 仅限数字值!) ;
}






我想计算学生的平均值和显示列表所有学生姓名,分数,最低和最高分标记为



For那你需要在某个地方保留一个学生或MyStudent对象的集合 - 你熟悉数组,或者通用列表< T>收藏?



[编辑]忘了分号![/ edit]



是的谢谢我熟悉数组但不熟悉通用列表集合。几天前才开始使用类.Unic的讲师不会让它更容易理解





OK - 阵列很简单,但是因为你需要事先说出它们将容纳多少元素,所以数组很有限。通用List更好,因为你可以动态添加和删除元素 - 但如果你还没有遇到它们,我会忽略它们,因为我不想过早介绍东西并混淆问题。

所以我们假设最多的学生为100 - 因为没有人想要输入那么多学生的详细信息 - 在你的主代码中你可以创建一个数组:



 MyStudent [] students =  new  MyStudent [ 100 ]; 




然后在循环中为每个您需要包含和填写的实际学生创建学生实例在它的详细信息:



  int  nextStudent =  0 ; 
while (moreStudentsToEnter)
{
MyStudent student = new MyStudent ();
student.GetStudentDetails();
学生[nextStudent ++] =学生;
...
}




然后在学生全部加入后,你可以循环学生并列出它们,平均它们的标记,无论你需要做什么:



  for < / span>( int  i =  0 ; i <  nextStudent; i ++)
{
Console.WriteLine(students [i] .NameOfStudent);
}




得到这个想法?

本文地址:IT屋 » 我正在使用类来获取用户的输入并显示它。请帮助更好地理解课程。