当前位置: 代码迷 >> Java相关 >> [求助]为何我编的程序只查找出第一个学生信息?
  详细解决方案

[求助]为何我编的程序只查找出第一个学生信息?

热度:286   发布时间:2006-06-05 19:23:00.0
[求助]为何我编的程序只查找出第一个学生信息?

题目:
编写一个学生类,要求有学号、姓名、性别、班级字段。要求实现构造函数和默认构造函数,并实现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)
{
}
}
}

这个就是他的程序 本来就应该是一个人啊 你要想全出来就按着顺序打印就好了 如果想从键盘输入的话 加上个 while循环就可以了


----------------解决方案--------------------------------------------------------
if((S.equals(sNum)))
{
System.out.println(e[i].show());
break;
}
else
{
System.out.println("对不起,你输入的学号不存在!");
break;
}
程序块里加Break是什么意思啊,有用吗,还是编程习惯!
----------------解决方案--------------------------------------------------------

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的话都显示输入的学号不存在,要怎么解决这个问题?


----------------解决方案--------------------------------------------------------
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;
}
}

这一句出了问题,你在比较了一遍后就break了,所以后面的循环就没有进行了
你应该判断一下,当i=e.length的时候,还没有匹配上才输出一个"对不起,你输入的学号不存在"这句话.

----------------解决方案--------------------------------------------------------
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;
}
}

这一句出了问题,你在比较了一遍后就break了,所以后面的循环就没有进行了
你应该判断一下,当i=e.length的时候,还没有匹配上才输出一个"对不起,你输入的学号不存在"这句话.


----------------解决方案--------------------------------------------------------
  相关解决方案