Hellooo 1과 정수 및 사용자 및 요소에 의해 입력 된 모든 완벽한 숫자를 표시하려고합니다. 난 괜찮아 그건 내 출력이 매우 내가 원하는 출력과 일치하지 않음을 그냥 작업 코드 ... 여기 자바 프로그래밍을 사용하여 완벽한 숫자
내 코드입니다 가지고 :import java.util.Scanner;
public class PerfectNumbers {
public static boolean isPerfect(int a) {
int n = a;
int sum = 0;
boolean perfect;
while (n-- >1) {
if(a%n==0)
sum = sum + n;
}
if (sum == a) {
perfect = true;
} else {
perfect = false;
}
return perfect;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number up to which you would like to look for perfect numbers:");
int i = input.nextInt();
String factors = "";
System.out.printf("Looking for perfect numbers from 1 to %d%n", i);
while (i > 0) {
if (PerfectNumbers.isPerfect(i) == true) {
int w = i-1;
while (w-- > 1) {
if(i % w == 0)
factors = factors + " " + Integer.toString(w);
}
System.out.println(i + " is a perfect number it's factors are:" + factors);
}
i = i - 1;
}
}
}
하지만 대신
가 가 가 가 가
각 정수 다음에 빈 문자열로 'factors'를 재설정해야합니다. – Steve
요인을 어떻게 "재설정"합니까? 이 의미는 빈 문자열과 동일합니까? 나는 초보자이므로 아직 확실하지 않다. – cossii
'while (i> 0)'대신'int j = 1;'을 추가하고 조건을'while (j <= i)'로 변경한다. 또한'i = i - 1;을'j = j + 1;'로 변경하십시오. 주문이 고쳐질 것입니다. – Gendarme