用c#编写一个程序,有5个学生,每个学生选修三门课,求每个学生成绩的平均分和总分。
用c#编写一个程序,有5个学生,每个学生选修三门课,求每个学生成绩的平均分和总分。程序要比较灵活...
用c#编写一个程序,有5个学生,每个学生选修三门课,求每个学生成绩的平均分和总分。程序要比较灵活
展开
展开全部
自己调调吧,就是丑点。
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//input names
string names = string.Empty;
while (true)
{
if (string.IsNullOrEmpty(names))
{
Console.WriteLine("Please input names(with a seprator ','):");
Console.WriteLine(" ex. zhangsan,lisi,wangwu)");
names = Console.ReadLine();
}
else
{
break;
}
}
//input grades
string grades = string.Empty;
while (true)
{
if (string.IsNullOrEmpty(grades))
{
Console.WriteLine("Please input grades(with seprators ',' and ';'):");
Console.WriteLine(" ex. 60,70,80;50,60,70;70,80,90)");
grades = Console.ReadLine();
}
else
{
break;
}
}
var nameList = names.Split(',');
var gradeList = grades.Split(';');
if(nameList.Length != gradeList.Length)
{
Console.WriteLine("Wrong students against grades.");
return;
}
List<int> gradePerStudent = new List<int>();
foreach(var g in gradeList)
{
var tmp = g.Split(',');
int gradePlused = 0;
foreach(var t in tmp)
{
gradePlused += int.Parse(t);
}
gradePerStudent.Add(gradePlused / tmp.Length);
}
for(int i = 0; i < nameList.Length; i++)
{
Console.WriteLine(nameList[i] + " : " + gradePerStudent[i]);
}
Console.ReadLine();
}
}
}
追问
谢谢
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询