using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace ConsoleApplication1
{
public class student
{
public string name { get; set; }
public int age { get; set; }
public int xuehao { get; set; }
public string sex { get; set; }
public string subject { get; set; }
public double score { get; set; }
public double average { get; set; }
public double Score
{
get { return score; }
}
public int Age
{
get { return age; }
}
public int Xuehao
{
get { return xuehao; }
}
public string Name
{
get { return name; }
}
public string Sex
{
get { return sex; }
}
public string Subject
{
get { return subject; }
}
public student()
{
}
public student(string name)
{
this.name = name;
}
public student(string name, int age)
{
this.name = name;
this.age = age;
}
public student(string name, int age, int xuehao)
{
this.name = name;
this.age = age;
this.xuehao = xuehao;
}
public student(string name, int age, int xuehao, string sex)
{
this.sex = sex;
this.xuehao = xuehao;
this.name = name;
this.age = age;
}
public student(string name, int age, int xuehao, string sex, string subject)
{
this.sex = sex;
this.xuehao = xuehao;
this.name = name;
this.subject = subject;
this.age = age;
}
public student(string name, int age, int xuehao, string sex, string subject, double score)
{
this.score = score;
this.sex = sex;
this.xuehao = xuehao;
this.name = name;
this.subject = subject;
this.age = age;
}
public student(string name, int age, int xuehao, string sex, string subject, double score,double average)
{
this.score = score;
this.sex = sex;
this.xuehao = xuehao;
this.name = name;
this.subject = subject;
this.age = age;
this.average = average;
}
public override string ToString()
{
return "学号:" + xuehao + " " + "名字:" + name + " " + "性别:" + sex + " " + "年龄:" + age + " " + "科目:" + subject + " " + "成绩:" + score + " " + "平均分:" + average;
}
}
public class Program
{
static void Main(string[] args)
{
ArrayList students = new ArrayList();
-
int i = Convert.ToInt32(Console.ReadLine());
for (int n = 1; n <= i; n++)
{
student s = new student();
Console.WriteLine("请输入学生名字:");
s.name = Console.ReadLine();
Console.WriteLine("请输入学生性别:");
s.sex = Console.ReadLine();
Console.WriteLine("请输入学生学号:");
s.xuehao = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入学生年龄:");
s.age = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请问您要输入几门科目成绩?");
int g = Convert.ToInt32(Console.ReadLine());
int count = 0;
double s2 = 0;
for (int m = 0; m < g; m++)
{
count++;
Console.WriteLine("请输入科目:");
s.subject = Console.ReadLine();
Console.WriteLine("请输入成绩:");
s.score = Convert.ToDouble(Console.ReadLine());
s2=s2+s.score;
students.Add(s);
}
s.average = s2 / count;
students.Add(s);
}
Console.WriteLine("请输入排序依据:");
string w = Console.ReadLine();
if (w == "平均分")
{
for (int x = 0; x < students.Count; x++)
{
for (int z = x + 1; z < students.Count; z++)
{
student a = (student)students[x];
student b = (student)students[z];
if (a.average > b.average)
{
object c = students[x];
students[x] = students[z];
students[z] = c;
}
}
}
}
{
for (int x = 0; x < students.Count; x++)//根据某一科目的分数排序
{
for (int z = x + 1; z < students.Count; z++)
{
student a = (student)students[x];
student b = (student)students[z];
if (w == a.subject && w == b.subject)
{
if (a.score > b.score)
{
object c = students[x];
students[x] = students[z];
students[z] = c;
}
}
}
}
}
if (w == "年龄")
{
for (int x = 0; x < students.Count; x++)
{
for (int z = x + 1; z < students.Count; z++)
{
student a = (student)students[x];
student b = (student)students[z];
if (a.age > b.age)
{
object c = students[x];
students[x] = students[z];
students[z] = c;
}
}
}
}
if (w == "学号")
{
for (int x = 0; x < students.Count; x++)
{
for (int z = x + 1; z < students.Count; z++)
{
student a = (student)students[x];
student b = (student)students[z];
if (a.xuehao > b.xuehao)
{
object c = students[x];
students[x] = students[z];
students[z] = c;
}
}
}
}
foreach (object o in students)
{
Console.WriteLine(o+" ");
}
}
}
}

为什么输入两门成绩,第一门会被覆盖掉?
其次为什么会有这么多行?

如果在第二个for循环中插入新的对象 student s1 = new student(); 则会出现以上结果
(省略以上代码)
```c#
Console.WriteLine("请问您要输入几门科目成绩?");
int g = Convert.ToInt32(Console.ReadLine());
for (int m = 1; m <= g; m++)
{
student s1 = new student();
Console.WriteLine("请输入科目:");
s1.subject = Console.ReadLine();
Console.WriteLine("请输入成绩:");
s1.score = Convert.ToDouble(Console.ReadLine());
students.Add(s1);
}
students.Add(s1);
}
(省略以下代码)
请问这些是因为什么,有办法解决他们吗?谢谢!
因为你应该用score1 score2 去存你的分数,不然肯定会覆盖掉。
你的结果图片获取不到。
不要放在code里。