带有测试的Java代码 – 无限循环?

我试图了解人与人之间的关系。 但是,当我运行unit testing时,测试会永远运行,它不会得到结果,而且我的CPU使用率很高。 有人能看出我的代码有什么问题吗?

字符串关系是字符串的多行输入,格式为"A , BC , D" ,其中AB的父节点, CD的父节点。

这是代码的默认构造函数和字符串格式的输入。 我们不需要检查格式是否正确:

 public SeeRelations(String relations){ this.relations = relations; } 

这个辅助函数从格式化的输入中获取字符串的每一行:

 //helper function to get each line of the string private ArrayList lineRelations(){ int i; ArrayList lineRelations = new ArrayList(); String[] lines = relations.split("\n"); for(i = 0; i < lines.length; i++){ lineRelations.add(lines[i]); } return lineRelations; } 

这是将输入格式化字符串中的所有关系放到arraylists的函数:

 //helper function to put each of the relationship in arraylists private ArrayList<ArrayList> allRelations(){ int i; ArrayList<ArrayList> allRelations = new ArrayList<ArrayList>(); ArrayList lineRelations = lineRelations(); for(i = 0; i < lineRelations.size(); i++){ ArrayList eachLine = new ArrayList(Arrays.asList(lineRelations.get(i).split("\\s*,\\s*"))); allRelations.add(eachLine); } return allRelations; } 

这是检查输入名称是否存在的方法:

 //helper function to see if the name exist for seeRelations() private boolean hasThisName(String name){ ArrayList<ArrayList> allRelations = allRelations(); int i; int j; for(i = 0; i < allRelations.size(); i++){ for(j = 0; j < allRelations.get(i).size(); j++){ if(name.equals(allRelations.get(i).get(j))){ return true; } } } return false; } 

这是获取两个人之间的世代号的function:

 //helper function to get Generation number of seeRelations() private int getGenerationNum(String person, String ancestor){ ArrayList<ArrayList> allRelations = allRelations(); String name; int i; int j; int generationNum = 0; for(i = 0, j = 0, name = ancestor; i < allRelations.size(); i++){ if(name.equals(allRelations.get(i).get(0)) && !person.equals(allRelations.get(i).get(1))){ generationNum++; ancestor = allRelations.get(i).get(1); i = 0; j = 1; } else if(ancestor.equals(allRelations.get(i).get(0)) && person.equals(allRelations.get(i).get(1))){ generationNum++; j = 1; break; } } if(j == 0){ return 0; } else{ return generationNum; } } 

这是为最终输出获得多个"great"的方法:

 private String great(int num){ int i; String great = ""; for(i = 0; i < num; i++){ great += "great"; } return great; } 

这是我检查两个人之间关系的最后方法:

 public String SeeRelations(String person, String ancestor){ int generationNum = getGenerationNum(person, ancestor); String great = great(generationNum - 2); if(!(hasThisName(person) && hasThisName(ancestor))){ return null; } else{ if(generationNum == 0){ return null; } else if(generationNum == 1){ return ancestor + " is the parent of " + person; } else if(generationNum == 2){ return ancestor + " is the grandparent of " + person; } else{ return ancestor + " is the" + " " + great +"grandparent of " + person; } } } 

这是我的测试用例,它永远运行,无法得到结果

 public class FamilyTreeTest { @Test public void testSeeRelations() { FamilyTree relation2 = new FamilyTree("John Doe , Mary Smith" + "\n" + "Martin Weasel , John Doe"); assertEquals("Martin Weasel is the grandparent of Mary Smith", familyTree2.SeeRelations("Mary Smith", "Martin Weasel")); } 

  for(i = 0, j = 0, name = ancestor; i < allRelations.size(); i++){ if(name.equals(allRelations.get(i).get(0)) && !person.equals(allRelations.get(i).get(1))){ generationNum++; ancestor = allRelations.get(i).get(1); i = 0; j = 1; } else if(ancestor.equals(allRelations.get(i).get(0)) && person.equals(allRelations.get(i).get(1))){ generationNum++; j = 1; break; } } 

在这里你有你的错误线你的祖先/名字是“马丁威瑟尔”给马丁的关系是“ name.equals(allRelations.get(i).get(0)) && !person.equals(allRelations.get(i).get(1))) ”,但你正在寻找玛丽史密斯所以name.equals(allRelations.get(i).get(0)) && !person.equals(allRelations.get(i).get(1)))这是true ,这个i = 0; 让你的循环从头开始

你可以做什么,尝试创建对象人,即类Person {String name; 列出儿童; 列出父母; ...}然后只做简单的树步行者

 int SearchDown(Person person, String searchedRelation,int generation) { if person.getName().equals(searchedRelation()) return generation; for (Person child: person.getChildren()) { int generation = SearchDown(child, searchedRelation, generation+1); if (generation!=-1) return generation; } return -1; } 

等等...

我真的觉得这种方式更容易处理所有类型的树木