日期:2014-05-20 浏览次数:20817 次
import java.util.Scanner; public class TeacherTest { public static void main(String[] args) { TeacherManager teacherManager = new TeacherManager(); Scanner scanner = new Scanner(System.in); System.out.println("请添加教师信息:"); int teacherId; String teacherName; int teacherDiscourse; int i, n = 1000;//最多只能存放1000个老师 Teacher teacher; String q; for (i = 0; i < n; i++) { System.out.println("教师编号:"); teacherId = scanner.nextInt(); System.out.println("教师姓名:"); teacherName = scanner.next(); System.out.println("教师论文数:"); teacherDiscourse = scanner.nextInt(); System.out.println("添加成功!"); teacher = new Teacher(teacherId, teacherName, teacherDiscourse); teacherManager.addTeacher(teacher, i); System.out.println("退出请按q,继续请按任意键:"); q = scanner.next(); if (q.equals("q") == true || q.equals("Q") == true) break; } n = i;//保存最后一位老师的下标 System.out.println("您输入的信息如下:"); Teacher[] teachers = teacherManager.getTeachers(); System.out.println("编号" + "\t" + "姓名" + "\t" + "论文数"); for (i = 0; i < n; i++) System.out.println(teachers[i]); System.out.println("论文大于20篇的教师信息如下:"); for (i = 0; i < n; i++) if (teachers[i].teacherDiscourse > 20) System.out.println(teachers[i]); } } class TeacherManager { Teacher[] teachers = new Teacher[1000]; public void addTeacher(Teacher teacher, int i) { teachers[i] = teacher; } public Teacher[] getTeachers() { return teachers; } } class Teacher { int teacherId; String teacherName; int teacherDiscourse; public Teacher(int teacherId, String teacherName, int teacherDiscourse) { super(); this.teacherId = teacherId; this.teacherName = teacherName; this.teacherDiscourse = teacherDiscourse; } public String toString() { return teacherId + " " + teacherName + " " + teacherDiscourse; } }