使用java编程的完美数字

时间:2016-11-05 07:39:28

标签: java

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; 
        } 
    } 
}

这是我的输出返回的内容 This is what my output returns

但是我希望它显示这个

But I want it to display this instead

5 个答案:

答案 0 :(得分:0)

在你的代码中考虑一下 在factors = ""; in while循环

中添加此项

这是工作代码

    public class PerfectNumbers { 

    public static boolean isPerfect(int a) { 
        int n = a; 
        int sum = 0; 
        boolean perfect = false; 

        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); 
                factors = "";
            } 
            i = i - 1; 
        }  
    }
}

答案 1 :(得分:0)

有趣的是,这是一个基于Java 8流的解决方案:

public class PerfectNumbers {
    public static void main(String[] args) {
        IntStream.range(1, 500)
                .filter(n -> factors(n).sum() == n)
                .forEach(n -> System.out.println(n + " is a prefect numbers its factors are "
                    + factors(n).mapToObj(Integer::toString).collect(Collectors.joining(" "))));
    }

    private static IntStream factors(int n) {
        return IntStream.range(1, n).filter(d -> n % d == 0);
    }
}

答案 2 :(得分:-1)

它应该是这样写的吗?

protected $except = [
    '*',
];

答案 3 :(得分:-1)

此代码工作正常。它给出了你预期的输出.. 公共类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) {
    // TODO Auto-generated method stub

    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(); 


    System.out.println("Looking for perfect numbers from 1 to "+i);
    int inc = 1;
    while (inc < i) { 

        if (PerfectNumbers.isPerfect(inc) == true) {
            String factors = "";
            int w = inc-1; 
            int j = 1;
            while (j < w) {

                if(inc % j == 0) 
                    factors = factors + " " + Integer.toString(j); 

                j++;
            } 
            System.out.println(inc + " is a perfect number it's factors are:" + factors); 

        } 
        inc = inc + 1; 
    }
    input.close();
}

}

答案 4 :(得分:-2)

你以相反的顺序循环,比如从1000到零。改变你的

    while (i>0)

    for (int j=1;j<i+1;j++)

因子循环

也是如此

修改

解决方案提案,仅在方法主要

中进行了更改
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 inp = input.nextInt(); 


   System.out.printf("Looking for perfect numbers from 1 to %d%n", inp);

   for(int j=1;j<inp+1;j++) { 

      if (PerfectNumbers.isPerfect(j) == true) {


         String factors = "";   
         for (int w=1; w<j;w++) {

         if(j % w == 0) 
            factors = factors + " " + Integer.toString(w); 

      } 
      System.out.println(j + " is a perfect number it's factors are:" + factors); 
    } 

    } 
}