10.编写一个控制台程序,重复输5个学生的信息(学生姓名,年龄,性别),并把这5个学生对象加入到StringBuffer中buffer.append(student),最后输出StringBuffer的内容,输出格式如下: 1.张三 22 男 2.李四 18 女 …… 注意每个学生的信息输出一行。
时间: 2023-05-25 17:03:56 浏览: 122
```
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
StringBuffer buffer = new StringBuffer();
Scanner scanner = new Scanner(System.in);
for (int i = 1; i <= 5; i++) {
System.out.println("请输入第" + i + "个学生的姓名:");
String name = scanner.next();
System.out.println("请输入第" + i + "个学生的年龄:");
int age = scanner.nextInt();
System.out.println("请输入第" + i + "个学生的性别(男/女):");
String gender = scanner.next();
Student student = new Student(name, age, gender);
buffer.append(student.toString() + "\n");
}
System.out.println(buffer.toString());
}
}
class Student {
private String name;
private int age;
private String gender;
public Student(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public String toString() {
return name + age + gender;
}
}
```
阅读全文