1 Bài 3 - Buổi 2 Sun Nov 04, 2012 4:05 pm
Admin
Admin
Bài 3:
(Write by MRKHOA92)
- Code:
import java.util.ArrayList;
import java.util.Scanner;
public class Bai3 {
ArrayList<String> Subject;
public Bai3(){
Subject = new ArrayList<String>();
}
//Them mon hoc
public void addSubject(String subject) {
Subject.add(subject);
}
//Kiem tra Su ton tai
public boolean checkExistOfSubjects(String subject) {
int l = Subject.size();
boolean flag = false;
for( int i = 0 ; i < l ; i++ ) {
if(subject.equals(Subject.get(i)) == true) {
flag = true;
break;
}
}
return flag;
}
//Hien thi thong tin
public void showInfomationOfSubjects() {
int l = Subject.size();
System.out.println("So Luong Cac Mon Hoc La: " + l);
for(int i = 0 ; i < l ; i++ ) {
System.out.println("Danh sach cac mon hoc: \n " + Subject.get(i));
}
}
public static void main(String [] args) {
Bai3 ob = new Bai3();
Scanner sc = new Scanner(System.in);
System.out.print("Nhap vao mon hoc can them: ");
String sj = sc.nextLine();
ob.addSubject(sj);
if(ob.checkExistOfSubjects(sj)) {
System.out.println(sj+ " ton tai. ");
}
ob.showInfomationOfSubjects();
}
}
(Write by MRKHOA92)
Được sửa bởi Admin ngày Thu Nov 08, 2012 9:40 pm; sửa lần 1.