编程题:阅读程序,按照题目需求修改程序
功能需求:
使用集合存储3个员工的信息(有序);
通过迭代器依次找出所有的员工。
提示:学生复制以下代码到编程区,并按需求进行调试修改。
in
// 1、导入相关包
//定义员工类
class Employee {
private String name;
private int age;
public Employee() {
super();
}
public Employee(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
//主函数
public class Main {
public static void main(String[] args) {
// 1、创建有序集合对象
Collection c ;
// 创建3个员工元素对象
for (int i = 0; i < 3; i++) {
Scanner sc = new Scanner(System.in);
String employeeName = sc.nextLine();
int employeeAge = sc.nextInt();
Employee employee = new Employee(employeeName, employeeAge);
c.add(employee);
}
// 2、创建迭代器遍历集合
Iterator it;
//3、遍历
while (it.hasnext) {
//4、集合中对象未知,向下转型
Employee e = it.next();
System.out.println(e.getName() + "---" + e.getAge());
}
}
}
### 输入样例:
在这里给出一组输入。例如:
in
zs
10
ls
20
ww
30
### 输出样例:
在这里给出相应的输出。例如:
out
zs---10
ls---20
ww---30
答案:若无答案欢迎评论
使用集合存储3个员工的信息(有序);
通过迭代器依次找出所有的员工。
提示:学生复制以下代码到编程区,并按需求进行调试修改。
in
// 1、导入相关包
//定义员工类
class Employee {
private String name;
private int age;
public Employee() {
super();
}
public Employee(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
//主函数
public class Main {
public static void main(String[] args) {
// 1、创建有序集合对象
Collection c ;
// 创建3个员工元素对象
for (int i = 0; i < 3; i++) {
Scanner sc = new Scanner(System.in);
String employeeName = sc.nextLine();
int employeeAge = sc.nextInt();
Employee employee = new Employee(employeeName, employeeAge);
c.add(employee);
}
// 2、创建迭代器遍历集合
Iterator it;
//3、遍历
while (it.hasnext) {
//4、集合中对象未知,向下转型
Employee e = it.next();
System.out.println(e.getName() + "---" + e.getAge());
}
}
}
### 输入样例:
在这里给出一组输入。例如:
in
zs
10
ls
20
ww
30
### 输出样例:
在这里给出相应的输出。例如:
out
zs---10
ls---20
ww---30
答案:若无答案欢迎评论