我刚刚开始研究2周前如何编码,我似乎在使用CharSequence时遇到了麻烦。
我正在编写基于电视节目“权力的游戏”的简单程序,它允许我使用ArrayList打印出姓名,他们的忠诚以及他们是否还活着或死亡。
这是我的角色类:
import java.util.ArrayList;
public class TestGoTCharacters {
public static void main(String[] args) {
// create an arraylist called character
ArrayList<Characters> character = new ArrayList<Characters>();
// character paramaters(name, allegiance, status, season appearance) into the arraylist
character.add(new Characters("Jon Snow", "House Stark, The Nights Watch", "Alive", 1));
character.add(new Characters("Neddard Stark", "House Stark", "Deceased", 1));
character.add(new Characters("Arya Stark", "House Stark, Faceless Men", "Alive", 1));
character.add(new Characters("Catelyn Stark", "House Stark", "Deceased", 1));
character.add(new Characters("Sansa Stark", "House Stark", "Alive", 1));
character.add(new Characters("Bran Stark", "House Stark", "Alive", 1));
character.add(new Characters("Rickon Stark", "House Stark", "Deceased", 1));
character.add(new Characters("Cersei Lannister","House Lannister, House Baratheon", "Alive", 1));
character.add(new Characters("Jamie Lannister", "House Lannister, Kingsguard", "Alive", 1));
character.add(new Characters("Tyrion Lannister", "House Lannister, House Targaryen", "Alive", 1));
character.add(new Characters("Tywin Lannister", "House Lannister", "Deceased", 1));
character.add(new Characters("Robert Baratheon", "House Baratheon, House Baratheon of Kings Landing", "Deceased", 1));
character.add(new Characters("Stannis Baratheon", "House Baratheon, House Baratheon of Dragonstone", "Deceased", 2));
character.add(new Characters("Renly Baratheon", "House Baratheon", "Deceased", 1 ));
character.add(new Characters("Melissandre", "The lord of light", "Alive", 2));
character.add(new Characters("Margaery Tyrell", "House Tyrell, House Baratheon, House Baratheon of Kings Landing", "Deceased", 2));
character.add(new Characters("Sandor 'The Hound' Clegane", "House Baratheon of Kings Landing, Brotherhood without Banners", "Alive", 1 ));
character.add(new Characters("Joffrey Baratheon", "House Baratheon of Kings Landing, House Lannister", "Deceased", 1));
character.add(new Characters("Gregor 'The Mountain' Clegane", "House Lannister", "Alive", 1));
character.add(new Characters("Khal Drogo", "Dothraki", "Deceased", 1));
character.add(new Characters("Benjen Stark", "House Stark, The Nights Watch", "Alive", 1));
character.add(new Characters("Ramsay Bolton", "House Bolton", "Deceased", 3));
character.add(new Characters("Daenarys Targayen", "House Targaryen", "Alive", 1));
character.add(new Characters("Viserys Targaryen", "House Targaryen", "Deceased", 1));
character.add(new Characters("Rhaegar Targaryen", "House Targaryen", "Deceased", 7));
character.add(new Characters("Petyr Baelish", "House Baelish", "Alive", 1));
character.add(new Characters("Robb Stark", "House Stark", "Deceased", 1));
character.add(new Characters("Theon Greyjoy", "House Targaryen, House Stark, House Greyjoy", "Alive", 1));
character.add(new Characters("Yara Greyjoy", "House Greyjoy", "Alive", 2));
/* method that reads out names in the array list */
/*
int count = 1;
System.out.println("Some of the characters in Game of Thrones, are: ");
for (int i = 0; i < character.size(); i++) {
System.out.println(count + ". " + character.get(i).getName());
count = count + 1;
}
*/
/* method that finds house allegiances */
System.out.println("\n"); // create a line space
System.out.println("The following characters are loyal to House Stark: ");
CharSequence HouseStark = "House Stark"; // if Allegiance contains House Stark then, print the name
for (int i = 0; i < character.size(); i++) {
// if (characer.get(i_.getallegiance() == "House Stark")
if (character.get(i).getallegiance() == HouseStark)// compares if allegiance paramters is true (equals house stark)
System.out.println(character.get(i).getName());
}
/* method that finds deceased */
/* method that finds characters seen in season 1 */
/* method that finds characters seen other than season 1 */
}
}
这是我的测试类
The following characters are loyal to House Stark:
Neddard Stark
Catelyn Stark
Sansa Stark
Bran Stark
Rickon Stark
Robb Stark
这就是它的内容,但我似乎注意到“Jon Snow”这样的字符不在输出中,即使我在参数“House Stark,The Nights Watch”中提及
{{1}}
答案 0 :(得分:0)
你在“Jon Snow”的忠诚中有一个逗号(超过一个值,所以它们不相等)。 allegiance
是String
,因此可以使用String.contains
if (character.get(i).getallegiance().contains(HouseStark))