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

时间:2012-11-18 21:52:40

标签: java

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

字符串关系是字符串的多行输入,格式为"A , B C , D",其中AB的父级,C是{{1}的父级}}

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

D

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

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

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

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

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

//helper function to put each of the relationship in arraylists
private ArrayList<ArrayList<String>> allRelations(){
    int i;
    ArrayList<ArrayList<String>> allRelations = new ArrayList<ArrayList<String>>();
    ArrayList<String> lineRelations = lineRelations();
    for(i = 0; i < lineRelations.size(); i++){
        ArrayList<String> eachLine = new ArrayList<String>(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<String>> 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;
}

这是为最终输出获取多个//helper function to get Generation number of seeRelations() private int getGenerationNum(String person, String ancestor){ ArrayList<ArrayList<String>> 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;
          }
    }
}

1 个答案:

答案 0 :(得分: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;
            }
        }

这里有你错误的台词 在你的情况下,你的祖先/名字是“Martin Weasel”,因为马丁的关系是“John Doe”,但你正在寻找玛丽史密斯所以name.equals(allRelations.get(i).get(0)) && !person.equals(allRelations.get(i).get(1)))这是true而这i = 0;使得你的循环从头开始

你能做什么,尝试创造对象的人 即 class Person { 字符串名称; 列出儿童; 列出父母; ... } 然后就做简单的树步行者

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;
}

等...

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