모든 요소를 배열 목록으로 반환하는 메서드를 만들어야합니다. 내가 지금 당분간 붙어있어 도움이 될 것입니다.모든 요소를 출력 할 Java 배열 목록
/**
* Determines whether the number has factors.
*
* @return true iff the number has a factor
*/
public boolean hasMoreFactors()
{
if (number >= 2) {
return true;
} else {
return false;
}
// return (number >= 2);
}
/**
* Is number divisible by a given other number?
*
* @param otherNumber the number we test whether it divides the object's number
* @return true iff the number is divisible by otherNumber
*/
public boolean isDivisible(int otherNumber)
{
if (number % otherNumber == 0) {
return true;
} else {
return false;
}
}
/**
* Determine next factor.
* pre-condition: call only if hasMoreFactors
* returns true
*
* @return a factor of the object's number
*/
public int nextFactor()
{
int triedFactor = 2;
while (! isDivisible(triedFactor)) {
triedFactor = triedFactor+1;
}
number = number/triedFactor;
return triedFactor;
}
/**
* Print all factors of the generator's number on standard output.
*/
public void printAllFactors()
{
System.out.println("Factors of " + number);
while (hasMoreFactors()) {
System.out.println(nextFactor());
}
System.out.println("That's it.");
}
/**
* Main method: Read an integer and print all its factors.
*/
public static void main(String[] args)
{
System.out.print("Please enter a number greater or equal 2: ");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
System.out.println();
FactorGenerator gen = new FactorGenerator(num);
gen.printAllFactors();
}
}
조금 더 자세히 설명해주세요. 어떻게 붙어 있니? : D – Skurmedel
첫 번째로, 'return BooleanExpression;'을 if로 래핑하는 대신에. – unholysampler
[ "숙제 태그, 다른 소위 '메타'태그와 마찬가지로 이제는 권장하지 않습니다.]] (http://meta.stackexchange.com/q/10812) – Will