题目:
编写一个学生类,要求有学号、姓名、性别、班级字段。要求实现构造函数和默认构造函数,并实现show方法用来显示学生基本信息。创建一个学生类数组(长度为5),并在数组中存放在3个学生信息。从键盘输入学号,把相应的学生信息查找并显示出来。
[此贴子已经被作者于2006-6-5 22:17:20编辑过]
题目:
编写一个学生类,要求有学号、姓名、性别、班级字段。要求实现构造函数和默认构造函数,并实现show方法用来显示学生基本信息。创建一个学生类数组(长度为5),并在数组中存放在3个学生信息。从键盘输入学号,把相应的学生信息查找并显示出来。
[此贴子已经被作者于2006-6-5 22:17:20编辑过]
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class StudentTest {
public static void main(String[] args) {
int id = 0;
try {
System.out.print("Enter id: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
id = Integer.valueOf(br.readLine());
} catch(Exception e) {
}
StudentManager[] student = new StudentManager[5];
student[0] = new StudentManager(1,"a","W","001");
student[1] = new StudentManager(2,"b","M","003");
student[2] = new StudentManager(3,"c","W","001");
System.out.println(student[id-1].show());
}
}
class StudentManager {
private int id;
private String studentName,studentSex,studentClass;
StudentManager(int id,String studentName,String studentSex,String studentClass) {
this.id = id;
this.studentName = studentName;
this.studentSex = studentSex;
this.studentClass = studentClass;
}
StudentManager() {
}
String show() {
return id + " , " + studentName + " , " + studentSex + " , " + studentClass;
}
}
这个程序运行不得啊
import java.util.*;
import java.io.*;
class Student
{
String name;
String num;
String sex;
String cla;
public Student(String name,String num,String sex,String cla)
{
this.name=name;
this.num=num;
this.sex=sex;
this.cla=cla;
}
public Student()
{
}
public String show()
{
return "name=" + name + " "+ "num=" + num + " " +"sex=" +
sex +" " + "cla=" +cla;
}
public String getNum()
{
return num;
}
}
class Collage extends Student
{
String department;
public Collage(String name,String num,String sex,String cla,
String department)
{
super(name,num,sex,cla);
this.department=department;
}
public String show()
{
return super.show()+"department=" + department;
}
}
public class StudentTest
{
public static void main(String s[])
{
String S=" ";
String sNum=" ";
Student e[]=new Student[3];
e[0]=new Student("李四","001","男","1班");
e[1]=new Student("张三","002","男","1班");
e[2]=new Student("李强","003","女","2班");
for (int i=0;i<e.length;i++)
{
String sBuf=e[i].show();
System.out.println(sBuf);
}
System.out.println("请输入学号:");
try
{
BufferedReader din=new BufferedReader(new InputStreamReader(System.in));
S=din.readLine();
}
catch (Exception b)
{
}
for(int i=0;i<e.length;i++)
{
sNum=e[i].getNum();
if((S.equals(sNum)))
{
System.out.println(e[i].show());
break;
}
else
{
System.out.println("对不起,你输入的学号不存在!");
break;
}
}
try
{
System.in.read();
}
catch (IOException c)
{
}
}
}
运行这个程序,如果输入学号002或003的话都显示输入的学号不存在,要怎么解决这个问题?