请帮我完成我的任务
编写程序来测试下面定义的Person类。您的测试程序应该创建该类的两个实例(每个实例用于测试不同的构造函数)并测试每个方法。您还需要说明尝试从客户端类访问私有数据成员时的错误(为清楚起见,暂时将private修饰符更改为public并再次测试)。请参阅下面的屏幕截图,了解示例输出。
屏幕截图显示为:
p1 name = Not Given Age = 0 Gender = U
p2 name = Jane Doe Age = 0 Gender = F
p1 name = John Doe Age = 25 Gender = M
和
PersonTester.jave:20: name has private access in Person
System.out.println("p2 name = " + p2.name + "Age = " + p2.age + "Gender = " + p2.gender);
PersonTester.jave:20: age has private access in Person
System.out.println("p2 name = " + p2.name + "Age = " + p2.age + "Gender = " + p2.gender);
PersonTester.jave:20: gender has private access in Person
System.out.println("p2 name = " + p2.name + "Age = " + p2.age + "Gender = " + p2.gender);
3 errors
这是给定的课程
class Person {
// Data Members
private String name; // The name of this person
private int age; // The age of this person
private char gender; // The gender of this person
// Default constructor
public Person() {
name = "Not Given";
age = 0;
gender = 'U';
}
// Constructs a new Person with passed name, age, and gender parameters.
public Person(String personName, int personAge, char personGender) {
name = personName;
age = personAge;
gender = personGender;
}
// Returns the age of this person.
public int getAge( ) {
return age;
}
// Returns the gender of this person.
public char getGender( ) {
return gender;
}
// Returns the name of this person.
public String getName( ) {
return name;
}
// Sets the age of this person.
public void setAge( int personAge ) {
age = personAge;
}
// Sets the gender of this person.
public void setGender(char personGender) {
gender = personGender;
}
// Sets the name of this person.
public void setName( String personName ) {
name = personName;
}
} // end class
这就是我所做的
import java.util.Scanner;
public class Person_Tester {
static char gender;
public static void main(String [] args){
//Create Array
Person[] p1 = new Person[2];
Person[] p2 = new Person[1];
try(Scanner string_input = new Scanner(System.in);
Scanner int_input = new Scanner(System.in)) {
for (int i = 0; i < 3; i++) {
Person person1 = new Person();
Person person2 = new Person();
//Enter Details For Person1
System.out.println("P1 Name: ");
person1.setName(string_input.nextLine());
System.out.println("Age: ");
person1.setAge(string_input.nextInt());
System.out.println("Gender: ");
gender = string_input.next().charAt(0);
System.out.println();
//Enter Details For Person2
System.out.println("P2 Name: ");
person2.setName(string_input.nextLine());
System.out.println("Age: ");
person2.setAge(string_input.nextInt());
System.out.println("Gender: ");
gender = string_input.next().charAt(0);
System.out.println();
string_input.close();
int_input.close();
}
}//end for loop
}//end main method
}//end class
当我运行代码时,这就是我得到的
Name:
John Doe
Age:
22
Gender:
M
Name:
Age:
21
Gender:
F
Exception in thread "main" java.lang.IllegalStateException: Scanner closed
at java.util.Scanner.ensureOpen(Unknown Source)
at java.util.Scanner.findWithinHorizon(Unknown Source)
at java.util.Scanner.nextLine(Unknown Source)
at Person_Tester.main(Person_Tester.java:26)
Name:
我试过拿出
string_input.close();
int_input.close();
我得到了
Name:
john doe
Age:
22
Gender:
m
Name:
Age:
21
Gender:
f
Name:
Age:
21
Gender:
f
Name:
Age:
23
Gender:
m
Name:
Age:
它一直在运行,不让我输入名字
请帮我修理一下,我需要在下午1点之前完成它
答案 0 :(得分:0)
让我总结一下你写的内容,希望它会给你一些指示
resource = CreateResource();
for(...) {
UseResource();
resource.Close();
}
首先创建资源(string_input
和int_input
)。然后你进入循环。在循环中使用资源,然后关闭资源,然后再次使用资源,然后再次关闭它。
关闭资源后,您不应再次使用它(或关闭它)。我会推荐两件事之一:
答案 1 :(得分:0)
System.out.println("Age: ");
person1.setAge(Integer.parseInt(string_input.nextLine()));
System.out.println("Gender: ");
gender = string_input.nextLine().charAt(0);
始终使用nextLine,因为当您使用Scanner.nextInt()然后调用Scanner.nextLine()时,它将从您读取int的行返回该行的剩余部分,该行基本上是一个空字符串。< / p>
nextLine()的Javadoc
使此扫描程序超过当前行并返回跳过的输入。此方法返回当前行的其余部分,不包括末尾的任何行分隔符。该位置设置为下一行的开头。
由于此方法继续搜索输入以查找行分隔符,因此如果没有行分隔符,它可以缓冲搜索要跳过的行的所有输入。