을 반환은인쇄 및 나는 현재 아래의 질문에 일하고 있어요 올바른 목록
// It should return the greatest common factor
// between two numbers.
//
// Examples of greatestCommonFactor:
// greatestCommonFactor(6, 4) // returns 2
// greatestCommonFactor(7, 9) // returns 1
// greatestCommonFactor(20, 30) // returns 10
//
// Hint: start a counter from 1 and try to divide both
// numbers by the counter. If the remainder of both divisions
// is 0, then the counter is a common factor. Continue incrementing
// the counter to find the greatest common factor. Use a while loop
// to increment the counter.
내 코드는 내가 올바른 출력을 얻는 문제에 봉착
public static List greatestCommonFactor(int a, int b){
int i = 1 ;
List topnum = new ArrayList();
ArrayList <Integer> factor = new ArrayList<Integer>();
while (i <= a || i <= b){
i++;
}
if (a%i == 0 && b%i == 0){
factor.add(i);
}
else if (a%i <= 1 || b%i <= 1){
Collections.sort(factor);
List<Integer> topnum1 = factor.subList(factor.size() - 1, factor.size());
}
return topnum;
}
아래에 표시됩니다. 현재 이상한 출력으로 []
이 표시됩니다. 오류가 발생하지 않고 행에 topnum
을 가져올 수 없습니다. 따라서이 항목을 관리 할 수 있습니다.
누구나 topnum1
에서 요소를 인쇄하여이 자습서를 해결할 수있는 팁이 있습니까?
을 당신이 그것을 만든 후에 당신이 topnum''뭔가를하지 않을! 물론 그것은 빈 반환됩니다. –
작업과 코멘트를 이해할 때 함수 greaterCommonFactor()는 목록 대신 int를 반환해야합니다. 맞습니까? –