在一小时前发布一篇关于早期问题并在4分钟内得到回复的情况,这是一个疯狂的病,再试一次我的运气。程序有5种方法,我现在正在制作所有这些方法因此它们不会全部都在这里(由于先前的响应而将其放入)现在问题是我的最后一个方法实际上做了除了使print语句正常工作之外的其他方法。该方法不完整,它不仅需要找到所述数字的因子,而且还必须将它们存储在一个数组中,加上它们并确定数字是否完美。最后一个未显示的方法是打印每个数字的实际数组(如果有的话)。 “testperfect”是我遇到问题的方法,在eclipse中我得到一个错误,说这个方法必须返回类型为boolean的结果。 。 。任何形式的帮助将不胜感激
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 :(得分:1)
所有代码路径都必须返回您的返回类型
想象一下,如果您的for循环根本没有执行,因为您的i
和perfect
的值,在这种情况下,您的函数将不会返回任何内容。
不允许。
public static boolean testPerfect() //tests the number to see if is perfect
{
//...code...
for( int i = 1; i < perfect; i++ )
{
//..code..
}
return false;
}