를 실행할 수없는 이유를 나에게 가능한 중복을 찾을 수 있도록 수 : 그러나, 여기 는 일부 내 코드는
Java code with tests - infinite loop?
이다 \ 포맷"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;
}
}
}
입력 문자열 형식이 여러 줄로 된 경우 – user1834274
위의 설명에서 오타가있는 경우 올바른 형식은 "A, B"+ "\ n"+ "C, D"입니다. 여기서 A는 B의 부모 C는 D – user1834274
의 부모이다. 코드에'System.out.println()'을 추가하여 메소드 이름과 메소드 params를 출력하고 콘솔이 코드가 매달려있는 곳을 볼 수있다. 또는 Java 디버거를 사용하여 코드를 단계별로 실행하여 진행 상황을 확인하십시오. –