三十多了,当老师也有十多年了,最近突然想学点东西,3月4号开始看苏坤老师的视频,学了四天了,因为工作比较忙,只学到了数组这一部分,自己给自己出了个题:输入学生的姓名,语文成绩,数学成绩,英语成绩,输出学生的各科成绩和排名及总分和排名。
做了快两个小时才写到这水平。请各位大神指点一下有什么不足并说一下看完苏坤老师的基础视频后该学什么,是面向对象,还是数据结构和算法,不胜感激。
附源码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _02自我练习
{
public struct Student
{
public string name;//学生姓名
public int shuXue;//数学成绩
public int yuWen;//语文成绩
public int yingYu;//英语成绩
public int zongFen;//总分
public int sxP;//数学名次
public int ywP;//语文名次
public int yyP;//英语名次
public int zfP;//总分名次
}
class Program
{
static void Main(string[] args)
{
Student[] stu = new Student[3];
//获取学生姓名和成绩,未处理输入异常。
for (int i = 0; i < stu.Length; i++)
{
Console.WriteLine("请输入学生姓名:");
stu[i].name = Console.ReadLine();、
Console.WriteLine("请输入{0}的语文成绩:", stu[i].name);
stu[i].yuWen = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入{0}的数学成绩:", stu[i].name);
stu[i].shuXue = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入{0}的英语成绩:", stu[i].name);
stu[i].yingYu = Convert.ToInt32(Console.ReadLine());
stu[i].zongFen = stu[i].yuWen + stu[i].shuXue + stu[i].yingYu;
}
//冒泡法进行学生数学成绩排名,此处思考良久,最终想通原来只要交换结构体即可:)其他学科同样方法,等学了方法后再修改吧
Student[] temp = new Student[stu.Length];
for (int i=0; i < stu.Length-1;i++ )
{
for (int j = 0; j<stu.Length-1-i; j++)
{
if(stu[j].zongFen<stu[j+1].zongFen)
{
temp[j] = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = temp[j];
}
}
}
//将数学名次赋值给结构体中sxP,并解决分数相同的情况
for (int i = 0; i < stu.Length; i++)
{
if (i>0)
{
if (stu[i].shuXue == stu[i - 1].shuXue)
{
stu[i].sxP = i;
continue;
}
}
stu[i].sxP = i+1;
} //输出学生成绩
Console.WriteLine("学生\t语文\t数学\t数名\t英语\t总分");
for (int i = 0; i < stu.Length; i++)
{
Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", stu[i].name, stu[i].yuWen, stu[i].shuXue, stu[i].sxP, stu[i].yingYu, stu[i].zongFen);
}
Console.ReadKey();
}
}
}
做了快两个小时才写到这水平。请各位大神指点一下有什么不足并说一下看完苏坤老师的基础视频后该学什么,是面向对象,还是数据结构和算法,不胜感激。
附源码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _02自我练习
{
public struct Student
{
public string name;//学生姓名
public int shuXue;//数学成绩
public int yuWen;//语文成绩
public int yingYu;//英语成绩
public int zongFen;//总分
public int sxP;//数学名次
public int ywP;//语文名次
public int yyP;//英语名次
public int zfP;//总分名次
}
class Program
{
static void Main(string[] args)
{
Student[] stu = new Student[3];
//获取学生姓名和成绩,未处理输入异常。
for (int i = 0; i < stu.Length; i++)
{
Console.WriteLine("请输入学生姓名:");
stu[i].name = Console.ReadLine();、
Console.WriteLine("请输入{0}的语文成绩:", stu[i].name);
stu[i].yuWen = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入{0}的数学成绩:", stu[i].name);
stu[i].shuXue = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入{0}的英语成绩:", stu[i].name);
stu[i].yingYu = Convert.ToInt32(Console.ReadLine());
stu[i].zongFen = stu[i].yuWen + stu[i].shuXue + stu[i].yingYu;
}
//冒泡法进行学生数学成绩排名,此处思考良久,最终想通原来只要交换结构体即可:)其他学科同样方法,等学了方法后再修改吧
Student[] temp = new Student[stu.Length];
for (int i=0; i < stu.Length-1;i++ )
{
for (int j = 0; j<stu.Length-1-i; j++)
{
if(stu[j].zongFen<stu[j+1].zongFen)
{
temp[j] = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = temp[j];
}
}
}
//将数学名次赋值给结构体中sxP,并解决分数相同的情况
for (int i = 0; i < stu.Length; i++)
{
if (i>0)
{
if (stu[i].shuXue == stu[i - 1].shuXue)
{
stu[i].sxP = i;
continue;
}
}
stu[i].sxP = i+1;
} //输出学生成绩
Console.WriteLine("学生\t语文\t数学\t数名\t英语\t总分");
for (int i = 0; i < stu.Length; i++)
{
Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", stu[i].name, stu[i].yuWen, stu[i].shuXue, stu[i].sxP, stu[i].yingYu, stu[i].zongFen);
}
Console.ReadKey();
}
}
}
continue;
{
public string Name { get;set; } public Dictionary<Course, int> Scores { get; set; }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class StudentInfo
{
public string studentName { get; set; }
public List<int> studentScores { get; set; }
}
class Program
{
static void Main(string[] args)
{
string[] courseNameList = { "语文", "数学", "英语", "计算机" };
List<StudentInfo> list = new List<StudentInfo>();
Console.Write("学生数:");
int n = int.Parse(Console.ReadLine());
for (int i = 0; i < n; i++)
{
Console.Write("请输入姓名:");
string studentName = Console.ReadLine();
var studentInfo = new StudentInfo
{
studentName = studentName,
studentScores = courseNameList.Select(x =>
{
Console.Write("请输入{0}的{1}成绩", studentName, x);
return int.Parse(Console.ReadLine());
}).ToList()
};
list.Add(studentInfo);
}
foreach (var studentInfo in list)
{
Console.WriteLine("{0}\t{1}", studentInfo.studentName, string.Join("\t", studentInfo.studentScores));
}
for (int i = 0; i < courseNameList.Length; i++)
{
Console.WriteLine("按照{0}成绩排序:", courseNameList[i]);
foreach (var studentInfo in list.OrderBy(x => x.studentScores[i]))
{
Console.WriteLine("{0}\t{1}", studentInfo.studentName, string.Join("\t", studentInfo.studentScores));
}
}
}
}
}学生数:4
请输入姓名:学生1
请输入学生1的语文成绩100
请输入学生1的数学成绩74
请输入学生1的英语成绩82
请输入学生1的计算机成绩97
请输入姓名:学生2
请输入学生2的语文成绩71
请输入学生2的数学成绩50
请输入学生2的英语成绩80
请输入学生2的计算机成绩76
请输入姓名:学生3
请输入学生3的语文成绩76
请输入学生3的数学成绩77
请输入学生3的英语成绩85
请输入学生3的计算机成绩94
请输入姓名:学生4
请输入学生4的语文成绩68
请输入学生4的数学成绩90
请输入学生4的英语成绩67
请输入学生4的计算机成绩66
学生1 100 74 82 97
学生2 71 50 80 76
学生3 76 77 85 94
学生4 68 90 67 66
按照语文成绩排序:
学生4 68 90 67 66
学生2 71 50 80 76
学生3 76 77 85 94
学生1 100 74 82 97
按照数学成绩排序:
学生2 71 50 80 76
学生1 100 74 82 97
学生3 76 77 85 94
学生4 68 90 67 66
按照英语成绩排序:
学生4 68 90 67 66
学生2 71 50 80 76
学生1 100 74 82 97
学生3 76 77 85 94
按照计算机成绩排序:
学生4 68 90 67 66
学生2 71 50 80 76
学生3 76 77 85 94
学生1 100 74 82 97
Press any key to continue . . .
//获取学生姓名和成绩,未处理输入异常。
for (int i = 0; i < stu.Length; i++)
{
Console.WriteLine("请输入学生姓名:");
stu[i].name = Console.ReadLine();、
Console.WriteLine("请输入{0}的语文成绩:", stu[i].name);
stu[i].yuWen = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入{0}的数学成绩:", stu[i].name);
stu[i].shuXue = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入{0}的英语成绩:", stu[i].name);
stu[i].yingYu = Convert.ToInt32(Console.ReadLine());
stu[i].zongFen = stu[i].yuWen + stu[i].shuXue + stu[i].yingYu;
} //先按数学排序
var query = stu.OrderByDescending(x => x.shuXue).ToArray(); //求数序名次
for (int i = 0; i < query.Count(); i++)
{
if (i > 0)
{
if (query[i].shuXue == query[i - 1].shuXue)
{
query[i].sxP = query[i - 1].sxP;
continue;
}
}
query[i].sxP = i + 1;
} //在按总分排序
var res = query.OrderByDescending(x => x.zongFen).ToArray(); //输出学生成绩
Console.WriteLine("学生\t语文\t数学\t数名\t英语\t总分");
for (int i = 0; i < res.Length; i++)
{
Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", res[i].name, res[i].yuWen, res[i].shuXue, res[i].sxP, res[i].yingYu, res[i].zongFen);
}
Console.ReadKey();
Student定义为struct不合适……
class Student
{
public string Name { get; set; } // 姓名
public List<Course> Corse { get; set; } // 各科成绩
public Course Total { get; set; } // 总成绩,想不起好的办法弄的。。 public Student()
{
// 假定只有这三门课程
Corse = new List<Course>();
Corse.Add(new Course { Name="英语"});
Corse.Add(new Course { Name = "语文" });
Corse.Add(new Course { Name = "数学" });
Total = new Course {Name="总成绩"};
}
}class Course
{
/// <summary>
/// 课程名
/// </summary>
public string Name { get; set; }
/// <summary>
/// 成绩
/// </summary>
public float Score { get; set; }
/// <summary>
/// 排名
/// </summary>
public int Rank { get; set; }
}
static void Main(string[] args)
{
List<Student> students = new List<Student>();
for (int i = 0; i < 2; i++)
{
Student stu = new Student();
Console.Write("\n请输入第{0}个学生的姓名: ", i+1);
stu.Name = Console.ReadLine();
Console.WriteLine("\n各科成绩:");
stu.Corse.ForEach(
x =>
{
Console.Write(" {0}: ", x.Name);
x.Score = Convert.ToSingle(Console.ReadLine());
}
);
stu.Total.Score = stu.Corse.Sum(x => x.Score);
students.Add(stu);
}
Console.WriteLine("\n 初始化的数据:");
PrintScores(students); Console.WriteLine("\n 总成绩排序");
PrintScores(students.OrderByDescending(x => x.Total.Score).ToList<Student>()); Console.WriteLine("\n 依据英语排序"); // 单科索引需要在引用的时候确定,假设已知
PrintScores(students.OrderByDescending(x => x.Corse.ElementAt(1).Score).ToList<Student>()); SetRank(students);
Console.WriteLine("\n显示单科排名: - 数学");
Console.WriteLine("姓名\t成绩\t排名");
students.ForEach(s => Console.WriteLine("{0}\t{1}\t{2}", s.Name, s.Corse.Last<Course>().Score, s.Corse.Last<Course>().Rank)); Console.ReadLine();
}
// 显示学生成绩信息
static void PrintScores(List<Student> stus)
{
Console.WriteLine("\n学生的成绩信息:");
Console.WriteLine("姓名\t英语\t语文\t数学\t总成绩");
//students.ForEach(s => ShowScore(s));
stus.ForEach(
s => Console.WriteLine(
"{0}\t{1}\t{2}", s.Name,
string.Join("\t", s.Corse.Select(x => x.Score.ToString()).ToArray()),
s.Total.Score));
}
//各科的排名 -- ?
static void SetRank(List<Student> stus)
{
for (int i = 0; i < 3; i++)
{
var tmp = stus.OrderByDescending(x => x.Corse.ElementAt(i).Score);
int j = 0;
foreach (var item in tmp)
{
item.Corse.ElementAt(i).Rank = ++j;
}
}
}
英语: 1
语文: 2
数学: 3请输入第2个学生的姓名: sss各科成绩:
英语: 9
语文: 8
数学: 7 初始化的数据:学生的成绩信息:
姓名 英语 语文 数学 总成绩
aaa 1 2 3 6
sss 9 8 7 24 总成绩排序学生的成绩信息:
姓名 英语 语文 数学 总成绩
sss 9 8 7 24
aaa 1 2 3 6 依据英语排序学生的成绩信息:
姓名 英语 语文 数学 总成绩
sss 9 8 7 24
aaa 1 2 3 6显示单科排名: - 数学
姓名 成绩 排名
aaa 3 2
sss 7 1
不好意思。.NET 3.5(VS2008)没有对IEnumerable<T>的重载形式,必须转化为string[]。.NET 4.0+(VS2010, 2012)支持。
接下来可以做个小的winform程序,把学生的信息显示在datagridview里;或者把学生的信息写进数据库。.net winform的控件相对容易一些,textbox,combobo什么的用F12看看,就差不多了,datagridview比较复杂,你可以给datagridview加个右键菜单(删除什么的),里边再加几个下拉列表,看看多久能实现datagridview中下拉列表的联动(比如第一列下是班级,第二列下是对应班级的学生,当第一列的班级选择变化的时候,第二列里是前边这班的学生)