各位大侠。。
我想输入姓名,分数后,然后打印出来;
接下来求平均分,能够根据平均分来排名。
string[] lin =new string[3] {"英语","数学","c#"};
for (int i = 0; i < 5; i++)
{
Console.WriteLine("请输入您的姓名-------: {0}", i + 1);
name[i] = Convert.ToString(Console.ReadLine());
for (int j = 0; j < 3; j++)
{
Console.WriteLine("分别输入英语成绩:{0}",lin[j]);//引入课程;
num[i, j] = Convert.ToInt32(Console.ReadLine());//引入分数; }
}
我想输入姓名,分数后,然后打印出来;
接下来求平均分,能够根据平均分来排名。
string[] lin =new string[3] {"英语","数学","c#"};
for (int i = 0; i < 5; i++)
{
Console.WriteLine("请输入您的姓名-------: {0}", i + 1);
name[i] = Convert.ToString(Console.ReadLine());
for (int j = 0; j < 3; j++)
{
Console.WriteLine("分别输入英语成绩:{0}",lin[j]);//引入课程;
num[i, j] = Convert.ToInt32(Console.ReadLine());//引入分数; }
}
但不知道怎样利用平均分来排名;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 3; j++)
{ sum[i] += num[i, j];
} Console.WriteLine(); }
//接下来求平均分
Console.WriteLine("姓名----英语--------数学--------c#--------平均分-----");
for (int i = 0; i < 5; i++)
{
Console.Write("{0,-10}", name[i]);//输出姓名;
for (int j = 0; j < 3; j++)
{
Console.Write("{0,-10}",num[i,j]);
} Console.Write("{0,-20}", sum[i] / 3); Console.WriteLine();
}
//接下来是排名的过程;
using System;
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
IList<Student> lstStudents = new List<Student>(); for (int i = 0; i < 3; i++)
{
Student student = new Student();
Console.WriteLine("请输入姓名:");
student.Name = Console.ReadLine();
Console.WriteLine("请输入英语成绩:");
student.English = double.Parse(Console.ReadLine());
Console.WriteLine("请输入数学成绩:");
student.Math = double.Parse(Console.ReadLine());
Console.WriteLine("请输入C#成绩:");
student.CSharp = double.Parse(Console.ReadLine());
student.Average = student.CaclAverage();//计算平均分
lstStudents.Add(student);
} foreach (Student stu in lstStudents)
{
Console.WriteLine("姓名:" + stu.Name + ";英语:" + stu.English +
";数学:" + stu.Math + ";C#:" + stu.CSharp + ";平均分:" + stu.Average);
} //按平均分降序排序
for (int i = 0; i < lstStudents.Count - 1; i++)
{
for (int j = 1; j < lstStudents.Count; j++)
{
Student tempStudent = null;
if (lstStudents[i].Average < lstStudents[j].Average)
{
tempStudent = lstStudents[j];
lstStudents[j] = lstStudents[i];
lstStudents[i] = tempStudent;
}
}
} foreach (Student stu in lstStudents)
{
Console.WriteLine("姓名:{0};平均分:{1}", stu.Name, stu.Average);
} Console.ReadLine();
} } class Student
{
public string Name;
public double English;
public double Math;
public double CSharp;
public double Average;//平均分 //计算平均分
public double CaclAverage()
{
//实际应用中注意检查数据是否合理
double dSum = English + Math + CSharp;
return dSum / 3;
} public override string ToString()
{
return Name;
}
}
}
public class Student : IComparable
{
private string _name;
private int _math;
private int _chinese ;
private int _cshape ;
private double _average ;
public Student(string name, int m, int c, int cs)
{
this._name = name;
this._math = m;
this._chinese = c;
this._cshape = cs;
this._average = (m + c + cs) / 3;
}
public int CompareTo (object o) //此处实现排序规则,
{
Student st = (Student)o;
if (st._average > this._average)
return 1;
else if (st._average == this._average)
return 0;
else
return -1;
}
public override string ToString()
{
return this._name + "\t\t" + this._average.ToString();
}
} // using System.Collections;
class Program
{
static void Main(string[] args)
{
ArrayList studentlist = new ArrayList(5); //使用ArrayList 需添加 , using System.Collections;
studentlist.Add(new Student("小王", 90, 80, 85));
studentlist.Add(new Student("小于", 80, 85, 73));
studentlist.Add(new Student("小丽", 95, 90, 85));
studentlist.Add(new Student("李", 80, 60, 70));
studentlist.Add(new Student("陈", 80, 85, 92));
foreach (Student s in studentlist )
{
Console.WriteLine (s.ToString());
}
Console.WriteLine("-------------------------------------------");
studentlist.Sort();
foreach (Student s in studentlist)
{
Console.WriteLine(s.ToString());
}
Console.ReadLine();
}
}
其实你去看看msdn最好了。