ArrayList<student> arr = new ArrayList<>();
student a1 = new student("马云",13);
student a2 = new student("王健林",12);
student a3 = new student("迪丽热巴",15);
student a4 = new student("好长的名字",18);
student a5 = new student("啊啊啊啊啊啊啊",1);
arr.add(a1);
arr.add(a2);
arr.add(a3);
arr.add(a4);
arr.add(a5);
利用\t对齐的时候 字数不同的名字无法达到对齐的效果
System.out.println("姓名\t\t\t\t\t"+"年龄");
for (int i = 0; i <arr.size() ; i++) {
student s = arr.get(i);
// 利用ifelse 将名字最长判断为7个字
//如果不足七个字,则补上对应空格(一个中文字符对应两个空格)
//比如当名字是两个字时,补上五个" "
//只能判断中文名字 如果是英文还需先判断进行T_T
if(s.getName().length()<7){
int size = s.getName().length();
for (int j = 7; j >size ; j--) {
s.setName(s.getName()+" ");
}
}
System.out.println(s.getName()+"\t\t"+s.getAge());
}
}
利用if补全空格达到效果