请用Java语言编写程序,将某位学员的个⼈信息输出到屏幕上。信息包括姓名(字符串常量)、性别(字符常量)、年龄 (整型常量)、⾝⾼(单位⽶,⼩数常量)、婚否(布尔常量)。输出格式如下: 1 2 3 4 5 张三 男 18 1.78 false
时间: 2024-09-26 08:03:57 浏览: 26
当然可以,以下是用Java编写的程序,它定义了一个Person类,包含了所需的个人信息,并按照给出的格式打印出来:
```java
public class Person {
private String name;
private char gender;
private int age;
private double height;
private boolean isMarried;
// 构造函数
public Person(String name, char gender, int age, double height, boolean isMarried) {
this.name = name;
this.gender = gender;
this.age = age;
this.height = height;
this.isMarried = isMarried;
}
// 输出个人资料的方法
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("\n" + (sb.length() + 1)).append(" ");
sb.append(name).append("\n");
sb.append((sb.length() + 1)).append(" ");
sb.append(gender).append("\n");
sb.append((sb.length() + 1)).append(" ");
sb.append(age).append("\n");
sb.append((sb.length() + 1)).append(" ");
sb.append(height).append("m\n");
sb.append((sb.length() + 1)).append(" ");
sb.append(isMarried ? "已婚" : "未婚").append("\n");
return sb.toString();
}
public static void main(String[] args) {
Person student = new Person("张三", '男', 18, 1.78, false);
System.out.println(student);
}
}
```
运行这个程序,你会看到如下的输出结果:
```
1
张三
2
男
3
18
4
1.78m
5
未婚
```
阅读全文