有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java布尔返回问题/从放入数组的方法中获取因子

在一个小时前发布了一个关于早期问题的帖子,并在4分钟内收到了疯狂的回复后,我会再试试我的运气。程序有5种方法,我目前正在制作所有这些方法,所以它们不会全部出现在这里(由于之前的响应,将其放入)。现在的问题是,我得到的最后一种方法,实际上除了制作打印语句之外,还能做些其他事情,以正确工作。该方法是不完整的,它不仅需要找到所述数的因子,还需要将它们存储在一个数组中,将它们相加,然后确定该数是否完美。最后一个未显示的方法是打印每个数字的实际数组(如果有)。“testperfect”是我遇到问题的方法,在eclipse中,我收到一个错误,说这个方法必须以布尔类型返回结果。任何形式的帮助都将不胜感激

import java.util.Scanner;

public class tgore_perfect 
{
 private static int count;
 private static int perfect;
 public static void main ( String args [])
 {
    count = getNum ();
    //System.out.print(count);
    boolean check = validateNum();
    //System.out.print(check);
    while (validateNum() == false)
    {
    System.out.print ("Non-positive numbers are not allowed.\n");
    count = getNum();
    }
    if (validateNum() == true)
    {
    for ( int i = 0; i < count; i++ )
    {
        perfect = getPerfect();
        testPerfect();

    }
}
}



public static int getNum () //gets amount of numbers to process
 {
     Scanner input = new Scanner ( System.in );
     int counter;

         System.out.println ("How many numbers would you like to test? ");
         counter = input.nextInt();
     return counter;
 }

 private static boolean validateNum() //checks user input
 { 
 if (count <= 0)
 {
     return false;
 }
 else
 {
     return true;
 }
 }

 public static int getPerfect() //gets number to process
 {
 Scanner input = new Scanner ( System.in);
 perfect = -1;
 System.out.print ("Please enter a possible perfect number: ");
 return perfect = input.nextInt();
 }

 public static boolean testPerfect() //tests the number to see if is perfect
{
     int sum = 0;
     int x = 0;

     for( int i = 1; i < perfect; i++ )
 {
     if((perfect % i ) == 0 )
     {
         x = i;
         sum += x;

     }
     if( x == perfect)
     {
         return true;
     }
     else 
     {
         return false;
     }
}
}
}

共 (0) 个答案