mình có bài tập như thế này:
1.Nhập điểm của 5 sinh vien với 3 môn bất kỳ. Tính toán điểm tổng kết và học lực.
2. Đưa ra danh sách 5 học sinh đó kèm theo điểm TK và học lực.
3. Xếp lại thự tự theo điểm TK.
Dưới đây là code, mình vẫn đang làm dở...
PHP Code:
class Program
{
static void Main(string[] args)
{
//Nhap diem 3 mon Toan,Ly,Hoa cua 5 hoc sinh.Tinh diem TK,xep loai.
//Dua ra danh sach 5 hoc sinh (kem theo diem TK, xep loai).
//Xep thu tu.
string nhap, hl = null, hs = null;
float toan, ly, hoa, dtk;
int dem = 0, f;
do
{
dem++;
Console.WriteLine("\n");
Console.WriteLine("Nhap diem cua hoc sinh thu {0}:", dem);
//
Console.Write("Diem mon Toan: ");
nhap = Console.ReadLine();
toan = Convert.ToSingle(nhap);
//
Console.Write("Diem mon Ly: ");
nhap = Console.ReadLine();
ly = Convert.ToSingle(nhap);
//
Console.Write("Diem mon Hoa: ");
nhap = Console.ReadLine();
hoa = Convert.ToSingle(nhap);
//
if (dem == 1)
{
f = 1;
hs="Hoc sinh 1";
dtk = (toan + ly + hoa) / 3;
Console.WriteLine("-> Diem Tong Ket: {0}", dtk);
if (dtk < 5)
{
hl = "Yeu";
Console.WriteLine("-> Hoc luc: {0}", hl);
}
else if (dtk < 6.5)
{
hl = "Trung Binh";
Console.WriteLine("-> Hoc luc: {0}", hl);
}
else if (dtk < 8)
{
hl = "Kha";
Console.WriteLine("-> Hoc luc: {0}", hl);
}
else if (dtk <= 10)
{
hl = "Gioi";
Console.WriteLine("-> Hoc luc: {0}", hl);
}
else Console.WriteLine("-> ERROR !");
}
else if (dem == 2)
{
f = 2;
hs = "Hoc sinh 2";
dtk = (toan + ly + hoa) / 3;
Console.WriteLine("-> Diem Tong Ket: {0}", dtk);
if (dtk < 5)
{
hl = "Yeu";
Console.WriteLine("-> Hoc luc: {0}", hl);
}
else if (dtk < 6.5)
{
hl = "Trung Binh";
Console.WriteLine("-> Hoc luc: {0}", hl);
}
else if (dtk < 8)
{
hl = "Kha";
Console.WriteLine("-> Hoc luc: {0}", hl);
}
else if (dtk <= 10)
{
hl = "Gioi";
Console.WriteLine("-> Hoc luc: {0}", hl);
}
else Console.WriteLine("-> ERROR !");
}
else if (dem == 3)
{
f = 3;
hs = "Hoc sinh 2";
dtk = (toan + ly + hoa) / 3;
Console.WriteLine("-> Diem Tong Ket: {0}", dtk);
if (dtk < 5)
{
Console.WriteLine("-> Hoc luc: {0}", hl);
}
else if (dtk < 6.5)
{
hl = "Trung Binh";
Console.WriteLine("-> Hoc luc: {0}", hl);
}
else if (dtk < 8)
{
hl = "Kha";
Console.WriteLine("-> Hoc luc: {0}", hl);
}
else if (dtk <= 10)
{
hl = "Gioi";
Console.WriteLine("-> Hoc luc: {0}", hl);
}
else Console.WriteLine("-> ERROR !");
}
else if (dem == 4)
{
f = 4;
hs = "Hoc sinh 4";
dtk = (toan + ly + hoa) / 3;
Console.WriteLine("-> Diem Tong Ket: {0}", dtk);
if (dtk < 5)
{
hl = "Yeu";
Console.WriteLine("-> Hoc luc: {0}", hl);
}
else if (dtk < 6.5)
{
hl = "Trung Binh";
Console.WriteLine("-> Hoc luc: {0}", hl);
}
else if (dtk < 8)
{
hl = "Kha";
Console.WriteLine("-> Hoc luc: {0}", hl);
}
else if (dtk <= 10)
{
hl = "Gioi";
Console.WriteLine("-> Hoc luc: {0}", hl);
}
else Console.WriteLine("-> ERROR !");
}
else
{
f = 5;
hs = "Hoc sinh 5";
dtk = (toan + ly + hoa) / 3;
Console.WriteLine("-> Diem Tong Ket: {0}", dtk);
if (dtk < 5)
{
hl = "Yeu";
Console.WriteLine("-> Hoc luc: {0}", hl);
}
else if (dtk < 6.5)
{
hl = "Trung Binh";
Console.WriteLine("-> Hoc luc: {0}", hl);
}
else if (dtk < 8)
{
hl = "Kha";
Console.WriteLine("-> Hoc luc: {0}", hl);
}
else if (dtk <= 10)
{
hl = "Gioi";
Console.WriteLine("-> Hoc luc: {0}", hl);
}
else Console.WriteLine("-> ERROR !");
}
//
} while (dem < 5);
Console.WriteLine("\n");
Console.WriteLine("DANH SACH HOC SINH");
// Console.WriteLine(" {0} \t Diem TK: {1} \t Hoc luc: {2}", hs, dtk, hl);
Console.ReadLine();
}
}
Mình đang cố gắng tối giản hết mức biến có thể. Cũng vì thế mà phần đưa ra danh sách của 5 học sinh kèm theo điểm TK, học lực trở nên khó khăn vì mình cùng sử dụng 3 biến (hs,dtk,hl) cho 5 học sinh. Tức là chỉ sử dụng 1 câu lệnh
Console.WriteLine(" {0} \t Diem TK: {1} \t Hoc luc: {2}", hs, dtk, hl);
để đưa ra danh sách của 5 học sinh với mối giá trị của biến hs,dtk,hl của 5 học sinh đó là khác nhau.
Mình nghĩ mãi mà không giải quyết được vấn đề này.
Chú ý: là không sử dụng mạng hay bất cứ gì đại loại như mảng.
THANK.