我要添加到此程序的最后一块是在打印结果后重复,直到按下EOF。这是我的代码
public static void main(String args[])
{
Scanner stdin = new Scanner(System.in);
System.out.println("Please enter a positive integer > 2. Press EOF to quit: ");
int num = stdin.nextInt();
while(stdin.hasNextInt)
{
if(num < 3)
{
System.out.println("Your input " + num + " is not a positive integer > 2");
}
else
{
int prime = 0;int pnum = 0;int dnum = 0;int anum = 0;
for(int i = 2;i <= num;i++)
{
if(NumberClassifier.isPrime(i)==true)
{
prime++;
}
if(NumberClassifier.classify(i)==1)
{
pnum++;
}
else if(NumberClassifier.classify(i)==2)
{
dnum++;
}
else if(NumberClassifier.classify(i)==3)
{
anum++;
}
}
System.out.println("In the range of 2 - " + num + " there are:");
System.out.println(prime + " prime numbers");
System.out.println(pnum + " perfect numbers");
System.out.println(anum + " abundant numbers");
System.out.println(dnum + " deficient numbers");
System.out.println("Please enter a positive integer > 2. Press EOF to quit: ");
}
}
//System.out.println("Please enter a positive integer > 2. Press EOF to quit: "); ( dont know where this should be)
}
此程序采用整数运行范围2 - 整数到2的方法计算范围内的素数,丰富,不足和完美数。我只想打印输入然后再次打印指令并运行直到按下EOF。我尝试使用while循环只继续程序,而扫描仪有一个整数,但似乎根本没有工作。 任何帮助将不胜感激。
答案 0 :(得分:0)
java.util.Scanner.hasNextInt
是一种方法,因此您必须调用它才能使用它。更改
int num = stdin.nextInt();
while(stdin.hasNextInt)
{
到
while(stdin.hasNextInt())
{
int num = stdin.nextInt();