2012-11-18 4 views
-1

를 실행할 수없는 이유를 나에게 가능한 중복을 찾을 수 있도록 수 : 그러나, 여기 는 일부 내 코드는


Java code with tests - infinite loop?

가 나는 사람들 사이의 관계를 얻을 원하는 내 코드, 때 나는 단위 테스트를 실행합니다. 테스트는 영원히 계속되었고 결과를 얻을 수 없었고, 사용하는 CPU는 높았습니다. 내 코드는 다음과 같습니다. 누군가 그 문제가 무엇인지 볼 수 있었습니까? 문자열 관계 + n을 "+는 "이것이 C는, A가 B 및 C의 부모 D는 "D.

의 부모

이다 \ 포맷"A, B "와 문자열의 다중 라인 입력이다 형식이 정확한지 코드와의 기본 생성자는 문자열의 입력 포맷, 우리는

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

// 도우미 함수는 문자열의 각 행을 얻기 위해 체크 할 필요가 없습니다

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

// 각 도우미 함수를 넣는 도우미 함수 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; 
      } 

이 입력 이름 이름 (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; 
    } 

이 얻을 수있는 기능이다) 볼 // 도우미 함수 존재 있는지 확인하는 방법 이명 // 도우미 함수의 생성 번호는이가 만난입니다 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; 
     } 
    } 

를 얻을 수 있습니다 최종 출력

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

입력 문자열 형식이 여러 줄로 된 경우 – user1834274

+0

위의 설명에서 오타가있는 경우 올바른 형식은 "A, B"+ "\ n"+ "C, D"입니다. 여기서 A는 B의 부모 C는 D – user1834274

+0

의 부모이다. 코드에'System.out.println()'을 추가하여 메소드 이름과 메소드 params를 출력하고 콘솔이 코드가 매달려있는 곳을 볼 수있다. 또는 Java 디버거를 사용하여 코드를 단계별로 실행하여 진행 상황을 확인하십시오. –

답변

1

이 코드 조각이 나에게 의심스러운 사이의 관계를 확인하는 나의 마지막 방법이다. i를 증가시킬 때 종료에 의존하는 루프 내부에 있지만 조건부로 i를 0으로 다시 설정합니다. 내가 과거 1 세를 보장 할 수있는 것은 무엇인가?

일반적으로 코드가 작동 할 때까지 코드를 단순화 한 다음 점진적으로 추가하면 한 번에 작은 코드 조각 만 디버깅하면됩니다.

+0

아니오 여러 번 확인했지만 적어도 나를위한 무한 루프가 아닌 것 같습니다 – user1834274

+0

그런 경우 실행해야 할 항목이있을 때까지 스트립 핑을 권장합니다. 그런 다음 점진적으로 테스트를 수행하십시오. –

관련 문제