根据总分排序
学生成绩包括chinese math english physic history
一个人五门成绩
姓名,成绩1 ,成绩2,等(txt文件)
输出txt文件是
姓名,成绩1 ,成绩2,等,总分
ChatGPT尝试解答您的问题,仅供参考
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Student
{
public string Name { get; set; }
public int Chinese { get; set; }
public int Math { get; set; }
public int English { get; set; }
public int Physics { get; set; }
public int History { get; set; }
public int TotalScore
{
get
{
return Chinese + Math + English + Physics + History;
}
}
}
class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>();
using (StreamReader reader = new StreamReader("students.txt"))
{
string line;
while ((line = reader.ReadLine()) != null)
{
string[] fields = line.Split(',');
if (fields.Length < 6)
{
Console.WriteLine("错误:" + line);
continue;
}
Student student = new Student
{
Name = fields[0],
Chinese = int.Parse(fields[1]),
Math = int.Parse(fields[2]),
English = int.Parse(fields[3]),
Physics = int.Parse(fields[4]),
History = int.Parse(fields[5])
};
students.Add(student);
}
}
using (StreamWriter writer = new StreamWriter("sorted_students.txt"))
{
foreach (Student student in students.OrderByDescending(s => s.TotalScore))
{
writer.WriteLine($"{student.Name},{student.Chinese},{student.Math},{student.English},{student.Physics},{student.History},{student.TotalScore}");
}
}
Student highest = students.OrderByDescending(s => s.TotalScore).First();
Console.WriteLine($"最高分:{highest.Name} {highest.TotalScore}");
Student lowest = students.OrderBy(s => s.TotalScore).First();
Console.WriteLine($"最低分:{lowest.Name} {lowest.TotalScore}");
}
}
在这段代码中,我们首先读取学生信息并将其存储在列表中。然后,我们将这些学生按照总分从高到低排序,并将结果写入新文本文件。最后,我们找到了最高分和最低分的学生,并在控制台中输出他们的信息。
希望能帮助到您,答案仅供参考
定义两个类来实现,一个Student用于表示学生及各科成绩,一个MainClass用来读取文件、转换成Student对象、排序、输出等。
望采纳,谢谢!
class Student
{
public string name;
public double chinese;
public double math;
public double english;
public double physic;
public double history;
public double total;
}
class MainClass
{
static void Main(string[] args)
{
List<Student> students = new List<Student>();
try
{
// 读取学生成绩并转换
using (StreamReader sr = new StreamReader("学生成绩.txt"))
{
string line;
while ((line = sr.ReadLine()) != null)
{
string[] temp = line.split(",");
Student student = new Student();
student.name = temp[0];
student.chinese = System.Convert.ToDouble(temp[1]);
student.math = System.Convert.ToDouble(temp[2]);
student.english = System.Convert.ToDouble(temp[3]);
student.physic = System.Convert.ToDouble(temp[4]);
student.history = System.Convert.ToDouble(temp[5]);
student.total = student.chinese + student.math + student.english + student.physic + student.history;
students.add(student);
}
}
}
catch (Exception e)
{
Console.WriteLine("读文件出错");
Console.WriteLine(e.Message);
}
// 对学生集合进行排序,比如根据总分升序排序
students.Sort(
delegate(Student s1,Student s2)
{
return s1.total.CompareTo(s2.total); // 升序
}
);
// 将排序的学生成绩输出到文件
using (StreamWriter sw = new StreamWriter("成绩排序.txt"))
{
foreach (Student student in students)
{
sw.WriteLine(student.name + "," + student.chinese + "," + student.math + "," + student.english + "," + student.physic + "," + student.history + "," + student.total);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
-- chinese math english physic history
namespace ConsoleApplication1
{
class Student
{
string sname;
int chinese;
int math;
int english;
int physic;
int history;
public void inputinfo()
{
Console.Write("姓名:");
sname =Console.ReadLine();
Console.Write("语文:");
chinese = int.Parse(Console.ReadLine());
Console.Write("数学:");
math = int.Parse(Console.ReadLine());
Console.Write("英语:");
english = int.Parse(Console.ReadLine());
Console.Write("地理:");
physic = int.Parse(Console.ReadLine());
Console.Write("历史:");
history = int.Parse(Console.ReadLine());
}
public float Comsum()
{
return this.chinese + this.math + this.english + this.physic + this.history;
}
public void display()
{
Console.WriteLine("\t{0} \t{1} \t{2} \t{3} \t{4}",sname,chinese,math,english,physic,history,Comsum());
}
}
class Program
{
static void sort(int n,params Student[] p)
{
Student temp;
int pos = 0;
for (int i = 0; i < n - 1; i++)
{
pos = i;
for (int j = i + 1; j < n; j++)
{
if (p[j].Comsum() > p[pos].Comsum())
{
pos = j;
}
}//第i个数和最小的数p【pos】互换
temp = p[i];
p[i]=p[pos];
p[pos] = temp;
}
}
static void Main(string[] args)
{
int i, stuNum;
Console.WriteLine("输入学生个数:");
stuNum = int.Parse(Console.ReadLine());
Student[] stu=new Student[stuNum];
for(i=0;i<stuNum;i++)
stu[i]=new Student();
for(i=0;i<stuNum;i++)
{
Console.WriteLine("输入第{0}学生数据:",i+1);
stu[i].inputinfo();
}
Console.WriteLine("排序前:");
Console.WriteLine("\t姓名\t 语文\t 数学\t 英语\t 地理\t 历史\t 总分");
for (i = 0; i < stuNum;i++ )
{
Console.Write("序号{0}:",i);
stu[i].display();
}
sort(stuNum, stu);
Console.WriteLine("排序后:");
Console.WriteLine("\t姓名\t 语文\t 数学\t 英语\t 地理\t 历史\t 总分");
for (i = 0; i < stuNum; i++)
{
Console.Write("第{0}名",i+1);
stu[i].display();
}
Console.ReadLine();
}
}
}
参考:
using System;
using System.Collections.Generic;
using System.IO;
namespace SortStudentsByTotalScore
{
class Student
{
public string Name { get; set; }
public int Chinese { get; set; }
public int Math { get; set; }
public int English { get; set; }
public int Physic { get; set; }
public int History { get; set; }
public int TotalScore { get; set; }
}
class Program
{
static void Main(string[] args)
{
// 从文本文件中读取学生成绩
List<Student> students = ReadStudentsFromFile("students.txt");
// 按总分进行排序
students.Sort((s1, s2) => s2.TotalScore.CompareTo(s1.TotalScore));
// 输出学生成绩
foreach (Student s in students)
{
Console.WriteLine($"{s.Name} {s.Chinese} {s.Math} {s.English} {s.Physic} {s.History} {s.TotalScore}");
}
}
static List<Student> ReadStudentsFromFile(string fileName)
{
List<Student> students = new List<Student>();
using (StreamReader reader = new StreamReader(fileName))
{
string line;
while ((line = reader.ReadLine()) != null)
{
string[] tokens = line.Split();
Student s = new Student
{
Name = tokens[0],
Chinese = int.Parse(tokens[1]),
Math = int.Parse(tokens[2]),
English = int.Parse(tokens[3]),
Physic = int.Parse(tokens[4]),
History = int.Parse(tokens[5]),
TotalScore = int.Parse(tokens[1]) + int.Parse(tokens[2]) + int.Parse(tokens[3]) + int.Parse(tokens[4]) + int.Parse(tokens[5])
};
students.Add(s);
}
}
return students;
}
}
}
使用linq. 直接orderby