Em có
- 1 class monhoc.
- 1 class danhsachmonhoc bao gồm kiểu mảng chứa các môn học trong class monhoc.
- 1 class quản lý để chạy,phần nhập thì trong class quanly,phan xuat thi trong cac class kia.
Cho hỏi code em sai chỗ nào ạ. Em nhập 1 môn học và xuất 1 môn học bình thường nhưng sao em nhập vào danh sách môn học và khi em tạo đối tượng gọi đến phương thức xuất lại bị lỗi ạ…
Class monhoc {
String tenmh;
int sotc;
float diemmh;
public monhoc()
{
}
public monhoc(String tenmh,int sotc,float diemmh)
{
this.tenmh = tenmh;
this.sotc = sotc;
this.diemmh = diemmh;
}
public String getTenmh() {
return tenmh;
}
public void setTenmh(String tenmh) {
this.tenmh = tenmh;
}
public int getSotc() {
return sotc;
}
public void setSotc(int sotc) {
this.sotc = sotc;
}
public float getDiemmh() {
return diemmh;
}
public void setDiemmh(float diemmh) {
this.diemmh = diemmh;
}
public void xuatmh()
{
System.out.println("ten mon hoc:" + getTenmh() + "\n" + "so tin chi:" + getSotc() + "\n" + "diem mon hoc:" + getDiemmh());
}
}
Class danhsachmonhoc {
int somonhoc;
monhoc[] dsmh;
public danhsachmonhoc()
{
somonhoc = 0;
}
public danhsachmonhoc(int somonhoc)
{
dsmh = new monhoc[somonhoc];
somonhoc++;
}
public int getSomonhoc() {
return somonhoc;
}
public void setSomonhoc(int somonhoc) {
this.somonhoc = somonhoc;
}
public monhoc[] getDsmh() {
return dsmh;
}
public void setDsmh(monhoc[] dsmh) {
this.dsmh = dsmh;
}
public void xuatdsmh(monhoc[] dsmh)
{
for(int i = 0;i<somonhoc;i++)
{
dsmh[i].xuatmh();//xuat danh sach mon hoc
somonhoc++;
}
}
}
Class quanly {
public static void main(String[] args) {
String ten;
int tc;
float diem;
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Nhap vao so luong mon hoc:");
n = sc.nextInt();
monhoc[] dsmh = new monhoc[n];
for(int i = 0;i<n;i++)
{
System.out.println("Nhap vao ten mon hoc:");
ten = sc.next();
System.out.println("Nhap vao so tin chi:"); //Nhap ds mon hoc
tc = sc.nextInt();
System.out.println("Nhap vao diem mon hoc:");
diem = sc.nextFloat();
dsmh[i] = new monhoc(ten,tc,diem);
}
danhsachmonhoc ds = new danhsachmonhoc();
ds.xuatdsmh();
}
}