2016-11-05 84 views
0

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

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

但我希望它显示这个代替

But I want it to display this instead

+0

您必须在每个完美数字后重置'因数'为空字符串 – Steve

+0

我将如何“重置”因数?这是否意味着将它等同于一个空字符串?我是初学者,所以我还不确定 – cossii

+0

而不是'while(i> 0)',添加'int j = 1;'并将条件改为'while(j <= i)'。还要把'i = i - 1;'改成'j = j + 1;'。这将解决排序问题。 – Gendarme

回答

-2

您环路以相反的顺序,说FR om 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); 
    } 

    } 
} 
+0

'while和'For'不是Java中的关键字。问题在于程序显示*错误*输出(248不是因子6),而不是输出顺序错误。 – Gendarme

+0

虽然和在java中不是关键字?你在说什么?????? – Massimo

+0

其实问题是程序显示错误的输出和错误的顺序:/ haha​​ – cossii

-1

它应该b e是这样写的吗?

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

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

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

       int w = i - 1; 

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

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

}

+1

为了避免内存泄漏,一旦完成,请关闭资源。 –

+0

为什么它没有用 –

+0

Aaah我看到我出错了!非常感谢你:) – cossii

0

做一个想在你的代码 在factors = "";在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; 
     } 
    } 
} 
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); 
    } 
}