完美数字 1 到 n

发布于 2024-10-18 16:24:53 字数 777 浏览 3 评论 0原文

编写一个算法来打印从 1 到 n 的完美数。要确定一个数字是否完美,请将该数字的所有小于该数字的因数相加。如果总和等于数字,那就完美了。

import java.util.Scanner;

public class Assign_6 {

    public static void main(String[] args){

        int num,number,sum=0,factor;

        System.out.print("Enter Number");
        Scanner keyboard = new Scanner (System.in);

        number=keyboard.nextInt();

        for (num=1;num<number;num++){
            for(factor=1;factor<number;factor++){
                if(num%factor==0){
                    sum= sum+factor;
                }
                if(sum==num){
                    System.out.println(sum);
                }
                sum=0;
            }

        }
    }
}

输出:24

没有打印任何内容。不知道出了什么问题。我哪里错了?我只能使用 while、for 和 else-if 语句。

Write an algorithm that prints perfect numbers from 1 to n. To determine if a number is perfect add up all the factors of the number that are less than the number. If the sum is equal to the number, it is perfect.

import java.util.Scanner;

public class Assign_6 {

    public static void main(String[] args){

        int num,number,sum=0,factor;

        System.out.print("Enter Number");
        Scanner keyboard = new Scanner (System.in);

        number=keyboard.nextInt();

        for (num=1;num<number;num++){
            for(factor=1;factor<number;factor++){
                if(num%factor==0){
                    sum= sum+factor;
                }
                if(sum==num){
                    System.out.println(sum);
                }
                sum=0;
            }

        }
    }
}

Output: 24

Nothing prints out. Don't know whats wrong. Where am I going wrong? I can only use while, for, and else-if statements.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

小帐篷 2024-10-25 16:24:53

打印出每个数字的总和(而不仅仅是当 sum==number 时),您就能算出来。

(两条线索:正确的缩进有助于快速找到匹配的 { } 对。并且为变量提供描述性名称是值得的。)

Print out the sum for every number (and not just when sum==number) and you'll be able to figure it out.

(Two clues: correct indentation helps find matching { } pairs quickly. And it pays to give your variables descriptive names.)

·深蓝 2024-10-25 16:24:53

您的代码中有两个问题:

  1. 以下代码应该使用小于或等于比较,否则,您会错过n,只对(n-1)执行1

    for (num = 1;num < number;num++){
    

    应该是下面的代码。

    for (num = 1;num <= number;num++){
    
  2. 以下代码位于for(factor=1;...)内部,但它应该位于循环外部。否则,您实际上会检查 &每个因素的明确总和。

    if(sum==num){System.out.println(sum);} sum=0;} 
    

There are two problems in your code:

  1. The following code should use a less than or equal to comparison, otherwise, you miss the n, only do 1 to (n-1)

    for (num = 1;num < number;num++){
    

    It should be the following code.

    for (num = 1;num <= number;num++){
    
  2. The following code is inside of for(factor=1;...), but it should be outside of the loop. Otherwise, you actually check & clear sum, for each factor.

    if(sum==num){System.out.println(sum);} sum=0;} 
    
戈亓 2024-10-25 16:24:53

在纸上追踪你的程序。您的 sum 变量有问题。

Trace through your program on paper. There is an issue with your sum variable.

断舍离 2024-10-25 16:24:53

当您开始编码时很容易混淆循环,因为在本作业中您有 2 个 for 循环,并且它们执行不同的操作。作为提示,您的程序无法运行的原因是因为您将一个 for 循环中的内容放错了到另一个 for 循环中。

您的方向是正确的,但请确保您能够单独考虑这些 for 循环。也就是说,内循环的预期目的是什么?它实际上在做什么?外循环的预期目的是什么?它实际上在做什么?看起来在你的内部循环中你正在尝试总结 num 的因子。它实际上只是总结因素,还是有一些您忽略的副作用/错误?

It's very easy when you are starting out coding to confuse your loops, since in this homework you have 2 for loops and they do different things. As a hint, the reason your program is not working is because you have misplaced stuff from one for loop to the other.

You are in the right direction, but make sure you are able to think about those for loops separately. That is, what is the inner loop's intended purpose? What is it actually doing? What is the outer loop's intended purpose, and what is it actually doing? It looks like in your inner loop you are trying to sum up factors for num. Is it actually just summing up the factors, or are there some side effects/errors you overlooked?

若沐 2024-10-25 16:24:53

尝试这段代码它将帮助你:

import java.util.Scanner;

public class Test
{
   public static void main (String[] args)
   {
       Scanner in = new Scanner(System.in);
       int n = in.nextInt();
       for(int j=1; j<n; j++)
       {
           int counter = 0;
           for (int i = 1; i < j; i++)         
           {              
               if(j%i ==0)
               {
                   counter = counter + i;
               }
           }
           if(counter == j)
               System.out.println(j);//here you can print j or counter
       }
   }
}

Try this code it will help you:

import java.util.Scanner;

public class Test
{
   public static void main (String[] args)
   {
       Scanner in = new Scanner(System.in);
       int n = in.nextInt();
       for(int j=1; j<n; j++)
       {
           int counter = 0;
           for (int i = 1; i < j; i++)         
           {              
               if(j%i ==0)
               {
                   counter = counter + i;
               }
           }
           if(counter == j)
               System.out.println(j);//here you can print j or counter
       }
   }
}
ぺ禁宫浮华殁 2024-10-25 16:24:53
public class PerfectNumber {
public static void main(String args [])
{

    perfectNumber(6,200);
}

static void perfectNumber(int num1, int num2){

    int i; int j; int sum=0;

     for (i=num1;i<=num2;i++){
            for(j=1;j<=i/2;j++){
                if(i%j==0){
                    sum= sum+j;
                }

            }
            if(sum==i){
                System.out.println("The perfect number between two numbers: "+sum);
            } 
            sum=0;
        }


    }

}

public class PerfectNumber {
public static void main(String args [])
{

    perfectNumber(6,200);
}

static void perfectNumber(int num1, int num2){

    int i; int j; int sum=0;

     for (i=num1;i<=num2;i++){
            for(j=1;j<=i/2;j++){
                if(i%j==0){
                    sum= sum+j;
                }

            }
            if(sum==i){
                System.out.println("The perfect number between two numbers: "+sum);
            } 
            sum=0;
        }


    }

}

極樂鬼 2024-10-25 16:24:53
import java.util.*;
    class PerfectNumber

{

public static void main(String[]args)
{
    System.out.println("Enter the number upto which you want to print the perfect number");
    Scanner scan=new Scanner(System.in);
    int n=scan.nextInt();
    for(int i=1;i<=n;i++)
    {
        int sum=0;
        for(int j=1;j<=i;j++)
        {
            if(i%j==0)
            sum=sum+j;
        }
        if(sum==(2*i))  //checks for perfect number
        System.out.println(i);
    }

    }
}
import java.util.*;
    class PerfectNumber

{

public static void main(String[]args)
{
    System.out.println("Enter the number upto which you want to print the perfect number");
    Scanner scan=new Scanner(System.in);
    int n=scan.nextInt();
    for(int i=1;i<=n;i++)
    {
        int sum=0;
        for(int j=1;j<=i;j++)
        {
            if(i%j==0)
            sum=sum+j;
        }
        if(sum==(2*i))  //checks for perfect number
        System.out.println(i);
    }

    }
}
兰花执着 2024-10-25 16:24:53
#include<iostream>
using namespace std;
int main()
{
  int start,end;
  cin>>start>>end;
  for(int j = start;j<=end;j++)
  {
    int div=0;
   for(int i =1;i<j;i++)
  {
    if(j%i == 0)
    {
      div = div+i;
    }
  }
    if(div==j)
    {
      cout<<j<<" ";
    }
  }
 return 0; 
}
#include<iostream>
using namespace std;
int main()
{
  int start,end;
  cin>>start>>end;
  for(int j = start;j<=end;j++)
  {
    int div=0;
   for(int i =1;i<j;i++)
  {
    if(j%i == 0)
    {
      div = div+i;
    }
  }
    if(div==j)
    {
      cout<<j<<" ";
    }
  }
 return 0; 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文