构造Person类。包括姓名(name),性别(sex)和年龄(age)。提供所有属性的set和get函数,提供print函数打印其信息 输入描述: 姓名(name),性别(sex)和年龄(age) 输出描述: 用户信息 裁判测试程序样例: import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner scan = new Scanner(System.in); String name = scan.next(); String sex = scan.next(); int age = scan.nextInt(); Person p = new Person(); p.setName(name); p.setSex(sex); p.setAge(age); p.print(); scan.close(); } } /* 你的代码被嵌在这里 */
时间: 2023-06-26 14:06:23 浏览: 147
```
import java.util.Scanner;
class Person {
private String name;
private String sex;
private int age;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getSex() {
return sex;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void print() {
System.out.println("Name: " + name);
System.out.println("Sex: " + sex);
System.out.println("Age: " + age);
}
}
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String name = scan.next();
String sex = scan.next();
int age = scan.nextInt();
Person p = new Person();
p.setName(name);
p.setSex(sex);
p.setAge(age);
p.print();
scan.close();
}
}
```
阅读全文