C#控制台程序——学生信息管理系统(练习)

这里写了一篇简单的控制台程序,用于总结与参考(手动添加学生及录入成绩并且打印成绩表)

所使用到的东西

  • for循环
  • while循环
  • foreach循环
  • List 集合
  • 创建方法
  • 方法重载
  • 方法调用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PrintStuXinXI
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("**************欢迎来到学生管理系统***************");

            EXE e = new EXE();
            string str = "";
            while (str != "end")
            {
                Console.WriteLine("-----请选择-----");
                Console.WriteLine("0-> 查看信息");
                Console.WriteLine("1-> 新增学生");
                Console.WriteLine("2-> 修改信息");
                Console.WriteLine("3-> 退出系统");
                switch (Console.ReadLine())
                {
                    case "0":
                        e.exe0();
                        break;
                    case "1":
                        Console.WriteLine();

                        e.exe1();
                        break;
                    case "2":
                        Console.WriteLine("开始修改:");
                        e.exe2();
                        break;
                    default:
                        Console.WriteLine("退出系统成功!");
                        str = "end";
                        break;

                }
            }

        }
    }

    public class EXE
    {
        List Students = new List();
        List students = new List();
        #region 新增
        public void exe0()
        {
            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine("姓名\t性别\t年龄\t语文\t数学\t英语");
            foreach (var item in students)
            {
                Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", item.StuName, item.StuSex, item.StunAge, item.Sorce.Chinese, item.Sorce.Math, item.Sorce.English);
            }

            // return students;
        }
        #endregion
        #region 新增
        public void exe1()
        {


            int i = 1;
            string str = "";
            while (str != "ok")
            {
                Console.WriteLine("请输入第{0}个学生的姓名:", i);
                string name = Console.ReadLine();
                Console.WriteLine("请输入第{0}个学生的性别:", i);
                string sex = Console.ReadLine();
                Console.WriteLine("请输入第{0}个学生的年龄:", i);
                string age = Console.ReadLine();
                Students.Add(new Student { StuName = name, StuSex = sex, StunAge = age });
                i = i + 1;
                Console.WriteLine("输入 ok 停止新增,按空格键继续。。");
                str = Console.ReadLine();
            }
            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine("姓名\t性别\t年龄");
            foreach (var item in Students)
            {
                Console.WriteLine("{0}\t{1}\t{2}", item.StuName, item.StuSex, item.StunAge);
            }

            Console.WriteLine("现在为现有学生的各项成绩打分");
            List subjects = new List();
            for (int j = 0; j < Students.Count; j++)
            {
                Console.WriteLine("请为{0}的语文科目打分:", Students[j].StuName);
                float chinese = float.Parse(Console.ReadLine());
                Console.WriteLine("请为{0}的数学科目打分:", Students[j].StuName);
                float math = float.Parse(Console.ReadLine());
                Console.WriteLine("请为{0}的英语科目打分:", Students[j].StuName);
                float english = float.Parse(Console.ReadLine());


                subjects.Add(new Subject { Chinese = chinese, Math = math, English = english });

            }



            for (int z = 0; z < Students.Count; z++)
            {
                students.Add(new Student { StuName = Students[z].StuName, StuSex = Students[z].StuSex, StunAge = Students[z].StunAge, Sorce = subjects[z] });
            }
            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine("姓名\t性别\t年龄\t语文\t数学\t英语");
            foreach (var item in students)
            {
                Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", item.StuName, item.StuSex, item.StunAge, item.Sorce.Chinese, item.Sorce.Math, item.Sorce.English);
            }

            // return students;
        }
        #endregion
        #region 修改
        public void exe2()
        {

            Console.WriteLine("现在修改学生的性别,请输入他的姓名:");
            if (students.Count > 0)
            {
                string name1 = Console.ReadLine();
                int index = students.FindIndex(a => a.StuName == name1);
                if (index > -1)
                {
                    Console.WriteLine("{0}所在的学号(index)为:201900{1}", name1, index);
                    Console.WriteLine(students[index].StuSex);
                    if (students[index].StuSex == "女")
                        students[index].StuSex = "男";
                    else
                        students[index].StuSex = "女";
                    Console.WriteLine("修改成功");
                    Console.WriteLine("");
                    Console.WriteLine("");
                    Console.WriteLine("姓名\t性别\t年龄\t语文\t数学\t英语");
                    foreach (var item in students)
                    {
                        Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", item.StuName, item.StuSex, item.StunAge, item.Sorce.Chinese, item.Sorce.Math, item.Sorce.English);
                    }
                }
                else { Console.WriteLine("班级无该学生!"); }
            }
            else
            {
                Console.WriteLine("无学生信息");
            }


        }
        #endregion
    }

    public class Student
    {
        public string StuName { get; set; }
        public string StuSex { get; set; }
        public string StunAge { get; set; }
        public Subject Sorce { get; set; }
    }
    public class Subject
    {
        public float Chinese { get; set; }
        public float Math { get; set; }
        public float English { get; set; }
    }
}


----仅供参考

你可能感兴趣的:(C#,C#)