我写了一个解决方案(我不是生物学家,我不知道血型匹配是如何工作的),但它只是检查血液的确切字母是否匹配。我相信你可以自己修改这个。我做的另一件事不是使用布尔数组,而是使用了一个单独的类,其中包含两个匹配项,即捐赠者和参与者。因为我在 cmets 中解释的当前模型对我来说没有多大意义。在接收者多于捐赠者的情况下使用布尔数组没有意义,并且您还可以将单个捐赠者与多个接收者进行多次匹配..
Main.java
public class Main {
public static void main(String[] args) {
Participant donors[] = new Participant[3];
Participant receivers[] = new Participant[6];
ParticipantMatch match[] = new ParticipantMatch[6];
//populate donors
donors[0] = new Participant("O", "Amy");
donors[1] = new Participant("A", "Alice");
donors[2] = new Participant("B", "Ash");
//populate receivers
receivers[0] = new Participant("AB", "Joe");
receivers[1] = new Participant("A", "John");
receivers[2] = new Participant("O", "Jim");
receivers[3] = new Participant("B-", "Jace");
receivers[4] = new Participant("AB-", "Jack");
receivers[5] = new Participant("A","James");
//check for match
int matchcounter = 0;
for (int i = 0; i < donors.length; ++i) {
for (int j = 0; j < receivers.length; ++j) {
if (donors[i].matches(receivers[j])) {
match[matchcounter++] = new ParticipantMatch(donors[i], receivers[j]);
}
}
}
for (int i = 0; i < match.length; ++i) {
if (match[i] != null) {
System.out.println(match[i].toString());
}
}
}
}
Participant.java
public class Participant {
private String bloodtype;
private String name;
public Participant(String bloodtype, String name) {
this.bloodtype = bloodtype;
this.name = name;
}
public String getbloodtype() {
return bloodtype;
}
public String getname() {
return name;
}
public boolean matches(Participant p) {
return this.bloodtype.equals(p.bloodtype);
}
}
ParticipantMatch.java
public class ParticipantMatch {
private Participant donor;
private Participant receiver;
public ParticipantMatch(Participant donor, Participant receiver) {
this.donor = donor;
this.receiver = receiver;
}
public Participant getdonor() {
return donor;
}
public Participant receiver() {
return receiver;
}
public String toString() {
return "Donor: " + donor.getname() + "-Receiver: " + receiver.getname() + "-Bloodtype: " + donor.getbloodtype();
}
}
如果我们执行该模型,我们会看到 Amy 与 Jim 匹配,而 Alice 与 John 和 James 匹配
Donor: Amy-Receiver: Jim-Bloodtype: O
Donor: Alice-Receiver: John-Bloodtype: A
Donor: Alice-Receiver: James-Bloodtype: A
正如我已经提到的,我不知道血型匹配的工作原理,因此您需要编辑将它们匹配在一起的算法。我刚刚检查了实际字符串是否匹配。