아래 튜토리얼 질문을하려고합니다. If 문에서 변수를 확인할 수 없습니다.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Ex4_GreatestCommonFactor {
// This is the main method that is executed as
// soon as the program starts.
public static void main(String[] args) {
// Call the greatestCommonFactor method a few times and print the results
}
public static int greatestCommonFactor(int a, int b){
int i = 1 ;
while (i >= 1){
i++;
}
if (a%i == 0 && b%i == 0){
ArrayList factor = new ArrayList<Integer>();
factor.add(i);
}
else if (a%i <= 1 || b%i <= 1){
Collections.sort(factor);
List<Integer> topnum = factor.subList(factor.size() - 1, factor.size());
}
return topnum;
}
}
그래서 나는이 개 질문이 아래
// Create a method called greatestCommonFactor
// 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.
그리고 내 코드입니다.
1) 내 elseif 문에서 factor
을 변수로 해석 할 수없는 오류가 발생합니다. 이전 If 문에서 factor
ArrayList를이 elseif 문으로 "이전"하려면 어떻게해야합니까?
2) topnum
을 해결할 수없는 유사한 오류가 발생합니다. 내 메서드에서이 코드 줄의 배치 오류입니까, 아니면 완전히 다른 실수입니까?
ArrayList factor = new ArrayList (); –
Josh
저는 메서드의 처음 네 줄을 약간 걱정했습니다. '1'에서 시작한다면'i'는 어떻게 될 것이라고 생각합니까? 그리고'> = 1'이 될 때까지 늘리십시오. 그리고 왜 이것을합니까? –
그 동안 루프가 끝나지 않을 것입니다. – Ghokun