程序运行一切,直到Person2的givePhoneNum()方法在givePhoneNum方法中打印第一行代码后终止。
这里是主要的:
import PersonInfo.Person;
import java.util.Scanner;
public class PersonStuff {
static Scanner in=new Scanner(System.in);
public static void main(String[] args) {
Person person1=new Person();
Person person2=new Person(18,'f');
person1.name="Bob";
person1.age=21;
person1.enterGender();
person2.name="Jane";
person2.givePhoneNum();
person2.printPhoneNum();
}
}
这是Person类:
package PersonInfo;
import java.util.Scanner;
public class Person {
static Scanner in=new Scanner(System.in);
public String name;
public int age;
private char gender;
private String phoneNum;
public Person(){ //Constructor 1
}
public Person(int agePerson, char genderPerson){ //Constructor 2
this.age=agePerson;
this.gender=genderPerson;
}
public Person(String personName, int personAge){ //Constructor 3
this.name=personName;
this.age=personAge;
}
// Methods
public void givePhoneNum(){
System.out.print("Enter phone number for "+name+":");
String phoneNum=in.nextLine();
this.phoneNum = phoneNum;
}
public boolean checkGender(char g){
if (g=='m' || g=='f'){
return true;
}
else return false;
}
public void enterGender(){
System.out.println("Enter gender:");
char gender=in.next().charAt(0);
//System.out.println(gender);
this.gender=gender;
checkGender(gender);
if(checkGender(gender)==false){
enterGender();
}
}
public void printPhoneNum(){
System.out.println(phoneNum);
}
}
非常感谢任何帮助。 谢谢!