主题:各位,帮忙修改一下
[题目:
编写一个输入人员信息并存储到内存的程序,输入部分可参考InputFromConsole类,人员信息包括name, sex, age.并且依据age对人员列表重新升序排序。]
_______________________________________________________
下面这个是我自己写的,程序能够正常运行,但执行后,在输入第一个名字的时候就出错了,还请各位帮忙修改一下,是什么地方的问题,谢谢咯~~~~
______________________________________________________
import java.util.Scanner;
public class PersonMessage {
String[] Name= null;
String[] Sex = null;
int[] Age = null;
public void InputFormConsole(){ //录入信息
for(int i=0; ; i++){
System.out.print("请输入名字:");
Scanner scname= new Scanner(System.in);
String name = scname.next();
Name[i] = name;
System.out.print("请输入性别:");
Scanner scsex= new Scanner(System.in);
String sex = scsex.next();
Sex[i] = sex;
System.out.print("请输入年龄:");
Scanner scage = new Scanner(System.in);
int age = scage.nextInt();
Age[i] = age;
System.out.print("是否继续录入:(Y/N)");
Scanner scInput = new Scanner(System.in);
String Input = scInput.next();
if (Input.equals("Y")){ //判断是否继续录入信息
PersonMessage r = new PersonMessage();
r.InputFormConsole(); //继续录入信息
}else {
PersonMessage r1 = new PersonMessage();
r1.OrderAge(); //转到age排序
}//
}
}
public void OrderAge(){
String NameTemp,SexTemp;
int AgeTemp; //年龄升序排列
for(int i=0; i<Age.length; i++){
for(int j=i ;j<Age.length-1; j++){ //排序
if(Age[j+1] > Age[j]){
AgeTemp = Age[j+1];
Age[j+1] = Age[j];
Age[j] = AgeTemp;
NameTemp = Name[j+1];
Name[j+1] = Name[j];
Name[j] = NameTemp;
SexTemp = Sex[j+1];
Sex[j+1] = Sex[j];
Sex[j] = SexTemp;
}
}
}
for(int k=Age.length ;k >= 0 ; k-- ){
System.out.println("姓名:"+Name[k]+",性别:"+Sex[k]+",年龄:"+Age[k]);
}
}
/**
* @param args
*/
public static void main(String[] args) {
PersonMessage Person =new PersonMessage();
Person.InputFormConsole();
}
}
编写一个输入人员信息并存储到内存的程序,输入部分可参考InputFromConsole类,人员信息包括name, sex, age.并且依据age对人员列表重新升序排序。]
_______________________________________________________
下面这个是我自己写的,程序能够正常运行,但执行后,在输入第一个名字的时候就出错了,还请各位帮忙修改一下,是什么地方的问题,谢谢咯~~~~
______________________________________________________
import java.util.Scanner;
public class PersonMessage {
String[] Name= null;
String[] Sex = null;
int[] Age = null;
public void InputFormConsole(){ //录入信息
for(int i=0; ; i++){
System.out.print("请输入名字:");
Scanner scname= new Scanner(System.in);
String name = scname.next();
Name[i] = name;
System.out.print("请输入性别:");
Scanner scsex= new Scanner(System.in);
String sex = scsex.next();
Sex[i] = sex;
System.out.print("请输入年龄:");
Scanner scage = new Scanner(System.in);
int age = scage.nextInt();
Age[i] = age;
System.out.print("是否继续录入:(Y/N)");
Scanner scInput = new Scanner(System.in);
String Input = scInput.next();
if (Input.equals("Y")){ //判断是否继续录入信息
PersonMessage r = new PersonMessage();
r.InputFormConsole(); //继续录入信息
}else {
PersonMessage r1 = new PersonMessage();
r1.OrderAge(); //转到age排序
}//
}
}
public void OrderAge(){
String NameTemp,SexTemp;
int AgeTemp; //年龄升序排列
for(int i=0; i<Age.length; i++){
for(int j=i ;j<Age.length-1; j++){ //排序
if(Age[j+1] > Age[j]){
AgeTemp = Age[j+1];
Age[j+1] = Age[j];
Age[j] = AgeTemp;
NameTemp = Name[j+1];
Name[j+1] = Name[j];
Name[j] = NameTemp;
SexTemp = Sex[j+1];
Sex[j+1] = Sex[j];
Sex[j] = SexTemp;
}
}
}
for(int k=Age.length ;k >= 0 ; k-- ){
System.out.println("姓名:"+Name[k]+",性别:"+Sex[k]+",年龄:"+Age[k]);
}
}
/**
* @param args
*/
public static void main(String[] args) {
PersonMessage Person =new PersonMessage();
Person.InputFormConsole();
}
}