读书人

生手求解答

发布时间: 2013-07-16 22:38:05 作者: rapoo

新手求解答
我刚刚写了一个测试程序,意思是想对教师信息进行输入显示,在这里我用的是泛型是对象进行存储,在构造对象时相关参数使用Scanner进行格式化输入的,可是我的构造函数的参数有十个,Scanner语句也是十个,可是为什么我输了九个参数以后输入就停止了呢,而我给定十个参数又是没错的,我给出我的源码和测试情况,求大神指导。
package com.Person;
import java.util.*;


class Person{
Person(String name, String sex, int age, int birth_year, int birth_month, int birth_day){
this.name = name;
this.sex = sex;
this.age = age;
this.birth_year = birth_year;
this.birth_month = birth_month;
this.birth_day = birth_day;
}

public String toString(){
return "Name: " + name + "\n" +
"Sex: " + sex + "\n" +
"Brithday: " + birth_year + "-" + birth_month + "-" + birth_day;
}

protected String name;
protected String sex;
protected int age;
protected int birth_year;
protected int birth_month;
protected int birth_day;
}

class Teacher extends Person{
Teacher(String name, String sex, int age, int birth_year, int birth_month, int birth_day,
String institute, String professional, String salary_number, double salary){
super(name,sex,age,birth_year,birth_month,birth_day);
this.institute = institute;
this.professional = professional;
this.salary_number = salary_number;
this.salary = salary;
}

void setInstitute(String institute){
this.institute = institute;
}
void setProfessional(String professional){
this.professional = professional;
}
void setSalary(double salary){
this.salary = salary;
}
String getInstitute(){
return institute;
}
String getProfessional(){
return professional;
}
double getSalary(){
return salary;
}
public String toString(){
return super.toString() + "\n" + "Institute: " + institute + "\n" +
"Professional: " + professional + "\n" + "Salary_Number: " + salary_number +
"\n" + "Salary: " + salary + "\n";
}

private String salary_number;
private String institute;//院系
private String professional;//职称
private double salary;
}


class ThePeopleOfTheCollge {



public static void main(String[] args) {
// TODO Auto-generated method stub
Teacher teacher = new Teacher("Lucy","Female",30,1893,07,23,"Java","Doctor","18347294",6000.00);
/*Scanner in = new Scanner(System.in);
Collection<Teacher> teacher = new ArrayList<Teacher>();
while(true){
System.out.println("Please the teacher the information:");
try{
teacher.add(new Teacher(in.nextLine(), in.nextLine(), in.nextInt(), in.nextInt(),
in.nextInt(), in.nextInt(), in.nextLine(), in.nextLine(), in.nextLine(), in.nextDouble()));
in.nextLine();//!!!清除多余字符
}catch (InputMismatchException e) {
System.out.println("The input is wrong!");
}

System.out.println("Please chooice N/Y to continue!");
if(in.nextLine().equals("N")){ //此处不能直接用等于
break;
}

}
for(Iterator<Teacher> i = teacher.iterator(); i.hasNext();){
System.out.println(i.next());
}*/
System.out.println(teacher);
}
}
给定参数的测试结果:
Name: Lucy
Sex: Female
Brithday: 1893-7-23
Institute: Java
Professional: Doctor
Salary_Number: 18347294
Salary: 6000.0

使用我注掉的部分测试结果如下:
Please the teacher the information:
Lucy
Femal
30
1893
07
23
Java
Doctor
1234567
Please chooice N/Y to continue!
Y
Please the teacher the information:
Tom
Male
40
1793
05
13
C++
Doctor
124536656
Please chooice N/Y to continue!
N
Name: Lucy
Sex: Femal
Brithday: 1893-7-23
Institute:
Professional: Java
Salary_Number: Doctor
Salary: 1234567.0

Name: Tom
Sex: Male
Brithday: 1793-5-13
Institute:
Professional: C++
Salary_Number: Doctor
Salary: 1.24536656E8

希望大神给解答。谢谢。 Scanner??? 测试
------解决方案--------------------


给点建议,你贴这么长的代码谁看呀,假如别人贴了这么长的代码,你愿意看呀?重新组织一下语言,把关键的代码贴出来就行了
[解决办法]
输入有问题,应该这样,
Please the teacher the information:
Lucy
Femal
30 1893 07 23 Java
Doctor
1234567
6000.00
Please chooice N/Y to continue!
[解决办法]
主要是nextInt后面不能直接回车
[解决办法]
如下这样输入:


Please the teacher the information:
Lucy
Female
30 1893 07 23 Java
Doctor
119
6000
Please chooice N/Y to continue!
N

运行结果:

Name: Lucy
Sex: Female
Brithday: 1893-7-23
Institute: Java
Professional: Doctor
Salary_Number: 119
Salary: 6000.0


问题出在最后一个nextInt操作(23后面的字符串),后面的字符串不能直接回车输入,否则会读取这个回车符作为nextLine读取的内容。
建议都用nextLine处理,int型使用Integer.parseInt(String s) 转换
[解决办法]
public String nextLine()此扫描器执行当前行,并返回跳过的输入信息。 此方法返回当前行的其余部分,不包括结尾处的行分隔符。当前位置移至下一行的行首。

nextInt不会自动移植下一行。

你在输入完年月日数字后,当前位置还在数字行,再用nextLine还是取当前行当前位置到换行符之间的内容,所以为空,你可以按照楼上说的把字符串和数字在同一行输入。也可以在代码in.nextInt()后增加一个in.nextLine()让它执行一次换行。为了使输入格式统一化,你还可以把数字的接收也使用in.nextLine(),取到后再做整型转换。

读书人网 >J2SE开发

热点推荐