返回列表 發帖
  1. import java.util.Scanner;                        //import Scanner this function
  2. public class JPA03 {                             //make a class "JPA03"
  3.     public static void main(String[] args) {     //make a function "main"
  4.         int i, sum;                              //declare integers
  5.         System.out.printf("1~1000中的完美數有: ");   //print out "some things"
  6.         for(i=1; i<=1000; i++){                  //the loop for i to run "1 to 1000"
  7.             sum=0;                               //clear integer sum
  8.             for(int j=1; j<i; j++){              //the loop for getting all common factors
  9.                 if(i%j==0)                       //to check if the integer j is the common factor of i
  10.                         sum+=j;                  //if so, then add j to sum
  11.             }
  12.             if(i==sum){                          //to check is i equal to j
  13.                 System.out.printf("%d ",i);      //if so, then print the number
  14.             }
  15.         }
  16.         System.out.printf("\n");                 //print next line(enter)
  17.     }
  18. }
複製代碼

TOP

返回列表