【问题标题】:Perfect number method完美数法
【发布时间】:2014-06-18 20:53:44
【问题描述】:

这个程序应该接受用户输入并确定它是否是一个完美的数字。当我尝试编译它时,我得到错误 Method testPerfect in class scalvert_Perfect cannot be applied to given types;

  • testPerfect(num);
  • 需要:int,int
  • 找到:int
  • 原因:实际参数列表和形式参数列表的长度不同

我的代码:

import java.util.Scanner;

public class scalvert_Perfect
{
public static void main ( String args[] ) 
{
    Scanner input = new Scanner(System.in);
    int test;
    int num = 0;
    int counter = 0;

    do
    {
        System.out.print("How many numbers would you like to test? ");
        test = input.nextInt();

    }while(test < 1);

    do
    {
        System.out.print("Please enter a possible perfect number: ");
        num = input.nextInt();

        testPerfect(num);
        printFactors(num);

        counter++;

    }while(counter < test);
}


    public static boolean testPerfect(int num, int test)
    {
        int sum = 0;

        for(int i = 0; i < test ; i++)
            {
                if(num % i == 0)
                {
                sum += i;
                }       

            }
                if(sum == num)
                {
                return true;
                }
                else
                {
                return false;
                }
    }       

    public static void printFactors(int num)
    {
        int x;
        int sum = 0;

        for(int factor = num - 1 ; factor > 0; factor--)
            {
                x = num % factor;

                if (x == 0)
                {
                    sum = sum+factor;
                }
            }
                if(sum != num)
                {
                    System.out.printf("%d:NOT PERFECT",num);
                }

            if(sum == num)
                {       
                    System.out.printf("%d: ",num);

                        for(int factor=1; factor < num; factor++)
                            {       
                                x = num % factor;
                                if(x == 0)
                                {
                                    System.out.printf("%d ",factor);
                                }
                            }               
                }
                System.out.print("\n");
                sum = 0;
    }


}

【问题讨论】:

    标签: java methods types perfect-numbers


    【解决方案1】:

    因为这个,你的函数需要两个整数:

    public static boolean testPerfect(int num, int test)
    

    你在这里用 1 个整数调用它:

     testPerfect(num);
    

    顺便说一句,这正是错误所说的:

    功能:

    testPerfect(num);
    

    需要两个整数

    required :int, int
    

    但是你用一个来称呼它:

    found: int
    

    所以错误是因为参数的数量不正确:

    reason: actual and formal argument list differ in length
    

    【讨论】:

    • 感谢您对此的深入解释。
    【解决方案2】:

    代码应该通过testPerfect 方法的测试,因为该方法的签名是testPerfect(int,int)。最初的调用只是将一个int 传递给该方法。

     do
        {
            System.out.print("Please enter a possible perfect number: ");
            num = input.nextInt();
    
            testPerfect(num,test);
            printFactors(num);
    
            counter++;
    
        }while(counter < test);
    

    【讨论】:

    • @user1858350 很高兴我能帮上忙。
    【解决方案3】:

    使用

    testPerfect(int num, int test)
    

    你使用了一个参数

    【讨论】:

      【解决方案4】:

      testPerfect 方法有两个参数int numint test

      testPerfect(num); 应该会产生编译器错误,因为不存在 testPerfect(int)

      您可能需要传递两个参数 Like testPerfect(num, test)

      【讨论】:

        【解决方案5】:

        你的函数需要两个 int 参数传递给它,但你只传递一个:

        testPerfect(num);
        

        在调用时尝试将两个传递给它。

        【讨论】:

          【解决方案6】:

          你这样称呼:

          public static boolean testPerfect(int num, int test)
          

          像这样:

          testPerfect(num);
          

          您要么在调用中缺少参数,要么在方法签名中要求的参数过多。

          【讨论】:

            【解决方案7】:
             public static void main(String[] args) {
                int n=25;
                for(int i=0;i<=n;i++)
                {
                    int v= i*i;
                    if(v == n)
                    {
                        System.out.println("it is a perfect number");
                    }
                }
            }
            

            【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-06-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多