[C# Hướng đối tượng] Nhập xuất danh sách Học sinh(Dùng List)

2

Viết chương trình nhập ngày sinh, điểm toán, lý hóa, họ tên, mssv-nhập vào 1 danh sách các học sinh trong 1 lớp hoc, và xuất ra các học sinh đó

Class HocSinh

using System;
using System.Collections.Generic;
using System.Text;

namespace QLHS
{
 
    class HocSinh
    {
        private double Toan, Ly, Hoa;
        private string Mssv, HoTen,ngaysinh;

        public void NhapHocSinh()
        {
            Console.Write("\nnhap vao ho ten: ");
            HoTen = Console.ReadLine();

            Console.Write("\nnhap vao mssv: ");
            Mssv = Console.ReadLine();

            Console.Write("\nnhap ngay sinh: ");
            ngaysinh = Console.ReadLine();

            while (true)
            {
                try
                {
                    Console.Write("\nnhap diem toan: ");
                    Toan = double.Parse(Console.ReadLine());
                    if (Toan < 0 || Toan > 10)
                        Console.Write("\n(0<toan<10). xin nhap lai!");
                    else
                        break;
                }
                catch
                {
                    Console.Write("\nkieu du lieu sai.xin nhap lai!");
                }
            }
            while (true)
            {
                try
                {
                    Console.Write("\nnhap diem ly: ");
                    Ly = double.Parse(Console.ReadLine());
                    if (Ly < 0 || Ly > 10)
                        Console.Write("\n(0<Ly<10). xin nhap lai!");
                    else
                        break;
                }
                catch
                {
                    Console.Write("\nkieu du lieu sai.xin nhap lai!");
                }
            }
            while (true)
            {
                try
                {
                    Console.Write("\nnhap diem Hoa: ");
                    Hoa = double.Parse(Console.ReadLine());
                    if (Hoa < 0 || Hoa > 10)
                        Console.Write("\n(0<Hoa10). xin nhap lai!");
                    else
                        break;
                }
                catch
                {
                    Console.Write("\nkieu du lieu sai.xin nhap lai!");
                }
            }
        }
        public void XuatHocSinh()
        {
            Console.WriteLine("Ho ten: {0}|| Ma SV: {1}|| Ngay sinh:{2} || Diem toan: {3}|| Diem Ly:{4} ||Diem hoa:{5}|| ", HoTen, Mssv, ngaysinh, Toan, Ly, Hoa);

            //ngaysinh.XuatNgay();
        }
    }
}

 

Class Lophoc

using System;
using System.Collections.Generic;
using System.Text;

namespace QLHS
{
    class Lophoc
    {
       
        List<HocSinh> hs = new List<HocSinh>();
        HocSinh a;
        public void NhapLophoc()
        {
            Console.Write("Nhap so HS:");
            int n = int.Parse(Console.ReadLine());

           for(int i=0;i<n;i++)
            {
               a = new HocSinh();
                a.NhapHocSinh();
                hs.Add(a);
            }
        }

        public void XuatLophoc()
        {/*
            
            for(int i=0;i<hs.Count;i++)
            {
                HocSinh obj = hs[i] as HocSinh;
                obj.XuatHocSinh();
            }
            */
            foreach(HocSinh a in hs)
            {
                a.XuatHocSinh();
            }
        }
    }
}

 

Class Program

using System;

namespace QLHS
{
    class Program
    {
        static void Main(string[] args)
        {
            Lophoc lh = new Lophoc();
            lh.NhapLophoc();
            lh.XuatLophoc();
            Console.ReadLine();
        }
    }
}
2 Comments
  1. Tiền Phúc Khải says

    ❤️❤️❤️

    1. Văn Long says

      Thanks e

Leave A Reply

Your email address will not be published.