我在这个程序的开始时遇到了一些麻烦。它应该取一个数字并确定它是否完美。目前我有它询问要检查多少个数字,每当我输入任何正数时,它都会再次询问。而且,每当我为第二个问题输入负数时,程序就会结束并且不再提示。有想法该怎么解决这个吗?
import java.util.Scanner;
public class aermel_Perfect
{
public static void main ( String args [] )
{
int gN = getNum();
int gP = getPerfect();
}
public static int getNum() //Get amount of numbers to check
{
Scanner input = new Scanner ( System.in );
System.out.print( "How many numbers would you like to test? " );
int count = input.nextInt();
int perfect = 1;
boolean vN = validateNum(count, perfect);
return count;
}
public static boolean validateNum( int count, int perfect ) //Check if number is valid
{
if (( count <= 0) || ( perfect <= 0))
{
System.out.print( "Non-positive numbers are not allowed.\n");
}
else
{
return true;
}
return false;
}
public static int getPerfect() //Gets the numbers to test
{
Scanner input = new Scanner ( System.in );
int perfect = -1;
int count = getNum();
System.out.print("Please enter a perfect number: " );
perfect = input.nextInt();
boolean vN = validateNum(perfect, count);
return perfect;
}
}