Hellooo 1とユーザーとその要素によって入力された整数の間の完全な数字をすべて表示しようとしています。私はJavaプログラミングを使用した完全な数字
に...コードそれは私の出力はかなり私が所望の出力と一致していないだけということです細かい作業を持ってここに私のコードです:
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