A C E

Quản Lý Sinh Viên JaVa - ACE

 


Xin chào tất cả các bạn đã quay trở lại với blog của tôi, lâu lắm rồi tôi chưa có post bài nhỉ? do gần đây tôi tập chung cho cv, học hành nên khá là bận bịu...Hôm nay tôi sẽ chia sẻ source code về "Quản Lý Sinh Viên" trong Java.

Đầu tiên các bạn tạo một class sinhvien như trong hình xong các bạn gõ code bên dưới.


Code class sinhvien:


import java.util.Scanner;


public class sinhvien {

String msv;

String name;

double dtb;

String xeploai;

 

public sinhvien() {

}



public sinhvien(String msv, String name, double dtb, String xeploai) {

this.msv = msv;

this.name = name;

this.dtb = dtb;

this.xeploai = xeploai;

}

public void nhap() {

Scanner sc =  new Scanner(System.in);

System.out.print("Nhap MSV:");

msv = sc.nextLine();

System.out.print("Nhap Ten:");

name =  sc.nextLine();

System.out.print("Nhap Diem Trung Binh:");

dtb =  Double.parseDouble(sc.nextLine());

System.out.print("Xep Loai:");

loai();

}

@Override

public String toString() {

return "sinhvien [msv=" + msv + ", name=" + name + ", dtb=" + dtb + ", xeploai=" + xeploai + "]";

}

public void display() {

System.out.println(toString());

}


public void loai() {

if(dtb<5) {

System.out.println(xeploai= "yeu" );

}

else if(dtb<6.5) {

System.out.println(xeploai = " trungbinh");

}

else if(dtb<7.5) {

System.out.println(xeploai ="kha");

}

else {

System.out.println(xeploai = "gioi");

}

}


public String getMsv() {

return msv;

}



public void setMsv(String msv) {

this.msv = msv;

}



public String getName() {

return name;

}



public void setName(String name) {

this.name = name;

}



public double getDtb() {

return dtb;

}



public void setDtb(double dtb) {

this.dtb = dtb;

}



public String getXeploai() {

return xeploai;

}



public void setXeploai(String xeploai) {

this.xeploai = xeploai;

}

}

Tiếp theo các bạn tạo class tuyensinh trong java, sau đó gõ code bên dưới.


import java.util.Scanner;

public class tuyensinh {
public static void main(String[] args) {
sinhvien  sv[] = null;
Scanner sc = new Scanner(System.in);
int n ;
int luachon;
do 
                {
Menu();
luachon =  Integer.parseInt(sc.nextLine());
switch(luachon) 
                {
case 1:
System.out.print("Nhập số lương sinh viên: ");
        n =Integer.parseInt(sc.nextLine());
        sv = new sinhvien[n];
        for (int i=0;i<n;i++) 
        {
            System.out.println("Sinh viên thứ " + (i + 1)+": ");
            sv[i] = new sinhvien();
            sv[i].nhap();
        }
        break;
                case 2:
        for(int i = 0;i <sv.length ;i++) 
        {
        sv[i].display();
        }
        break;
case 3:
sinhvien tg = sv[0];
                for (int i = 0; i < sv.length - 1; i++) 
                {
                    for (int j = i + 1; j < sv.length; j++) 
                    {
                        if (sv[i].getDtb() > sv[j].getDtb()) 
                        {
                            tg = sv[j];
                            sv[j] = sv[i];
                            sv[i] = tg;
                        }
                    }
                }
                    System.out.println("Sau khi sap xep la:");
                for(int i = 0;i <sv.length ;i++) 
                {
                sv[i].display();
                } break;
case 4:
                    System.out.println("Exit.");
                    break;
            default :
                System.out.println("Không có trong chương trình, mời bạn nhập lại:");
                break;
                }
               
}while(luachon!=4);
}
static void Menu() 
        {
System.out.println("Mời bạn lựa chọn trong các số sau:");
System.out.println(" CHUONG TRINH QUAN LY TUYEN SINH ");
System.out.println(" 1.Nhap sinh vien. ");
System.out.println(" 2.Hien thi sinh vien. ");
System.out.println(" 3.sap xep. ");
System.out.println(" 4.Thoat. ");
}
}
Sau khi hoàn thành các bạn nhân shift f6 trong phần tuyển sinh để chạy thôi...
Thanks tất cả các bạn đã đọc hết, phần sau mình sẽ ra nhiều hơn về các bài tập cũng như những điều hay ho thú vị trong lập trình...Các bạn nhớ theo dõi thường xuyên nhé!

Nhận xét