初学第四天,自己出了个题做了做,请各位指点
三十多了,当老师也有十多年了,最近突然想学点东西,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 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 . . .
[解决办法]
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;
}
//先按数学排序
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();