我有一个文件,其中包含一个名单及其性别列表以及有多少人拥有该名称的计数。我想并排打印十大女性和男性名字。我的问题是,当我运行我的程序时,这就是它打印的内容。
什么是打印
"Female" "Male"
Jacob Jacob
Ethan Ethan
Michael Michael
Jayden Jayden
William William
Alexander Alexander
Noah Noah
Daniel Daniel
Aiden Aiden
Anthony Anthony
我想要打印
"Female" "Male"
Isabella Jacob
Sophia Ethan
Emma Michael
Addison William
Elizabeth Alexander
Ella Joshua
Olivia Mason
Ava Evan
Emily Nicholas
Abigail Gavin
for (String s : descending())
{
for (int i = 0 ;i < 10;i++)
{
String z = oneName.get(i).getName();
if('M' == oneName.get(i).getSex())
{
System.out.printf("%13.10s%10.10s%n",z, oneName.get(i).getName());
}
if('F' == oneName.get(i).getSex())
{
z = oneName.get(i).getName();
}
}
break;
}
答案 0 :(得分:1)
每次打印时,z
始终设置为当前名称......
String z = oneName.get(i).getName();
if('M' == oneName.get(i).getSex())
{
System.out.printf("%13.10s%10.10s%n",z, oneName.get(i).getName());
这表明女性名字似乎是第一位的。
假设数据按女性/男性顺序排列,您可以执行类似......
的操作String maleName = null;
String femaleName = null;
for (int i = 0; i < 10; i++) {
if ('M' == oneName.get(i).getSex()) {
maleName = oneName.get(i).getName();
}
if ('F' == oneName.get(i).getSex()) {
femaleName = oneName.get(i).getName();
}
if (maleName != null && femaleName != null) {
System.out.printf("%13.10s%10.10s%n", femaleName, maleName);
maleName = null;
femaleName = null;
}
}
但是我对数据的存储顺序做了假设,没有证据给国家,很难提出别的建议
更新了概念验证
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
List<Person> people = new ArrayList<>(25);
people.add(new Person("Isabella", 'F'));
people.add(new Person("Jacob", 'M'));
people.add(new Person("Sophia", 'F'));
people.add(new Person("Ethan", 'M'));
people.add(new Person("Emma", 'F'));
people.add(new Person("Michael", 'M'));
people.add(new Person("Addison", 'F'));
people.add(new Person("William", 'M'));
people.add(new Person("Elizabeth", 'F'));
people.add(new Person("Alexander", 'M'));
people.add(new Person("Ella", 'F'));
people.add(new Person("Joshua", 'M'));
people.add(new Person("Olivia", 'F'));
people.add(new Person("Mason", 'M'));
people.add(new Person("Ava", 'F'));
people.add(new Person("Evan", 'M'));
people.add(new Person("Emily", 'F'));
people.add(new Person("Nicholas", 'M'));
people.add(new Person("Abigail", 'F'));
people.add(new Person("Gavin", 'M'));
String maleName = null;
String femaleName = null;
for (Person p : people) {
if ('M' == p.getSex()) {
maleName = p.getName();
}
if ('F' == p.getSex()) {
femaleName = p.getName();
}
if (maleName != null && femaleName != null) {
System.out.printf("%13.10s%10.10s%n", femaleName, maleName);
maleName = null;
femaleName = null;
}
}
}
public static class Person {
private String name;
private char sex;
public Person(String name, char sex) {
this.name = name;
this.sex = sex;
}
public char getSex() {
return sex;
}
public String getName() {
return name;
}
}
}
打印出来......
Isabella Jacob
Sophia Ethan
Emma Michael
Addison William
Elizabeth Alexander
Ella Joshua
Olivia Mason
Ava Evan
Emily Nicholas
Abigail Gavin
答案 1 :(得分:0)
String z = oneName.get(i).getName();
在forLoop之外声明变量z,否则每次都会创建新的String。
String z = null;
for (String s : descending())
{
for (int i = 0 ;i < 10;i++)
{
z = oneName.get(i).getName();