请帮我完成我的任务
编写程序来测试下面定义的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 = gender;
}
// 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("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("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)
错误消息正确,因为Scanner对象实际上没有nextAt()
方法。它有.next()
,.nextInt()
,.nextLine()
,.nextDouble()
,......但没有.nextAt()
。这里的关键是当你遇到类似的问题时,检查Java API以查看 可用的方法。这是Scanner API部分的链接,它将显示扫描程序对象可用的所有方法/构造函数。
编译器非常严格且不可原谅,不允许使用伪造的方法,因此编码人员也必须严格执行。