[英]Programming is asking for input twice
I'm having some troubles with the beginning of this program. 我在程序开始时遇到了一些麻烦。 It's supposed to take a number and determine whether or not it is perfect.
应该用一个数字来确定它是否完美。 Currently I have it asking how many numbers to check, whenever I input any positive number, it asks again.
目前,我要查询多少个数字,每当输入正数时,它都会再次询问。 AND, whenever I input a negative number for the second question, the program ends and doesn't prompt again.
而且,每当我为第二个问题输入负数时,程序就会结束,并且不会再次提示。 Any ideas on how to fix this?
有想法该怎么解决这个吗?
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;
}
}
int count = getNum();
System.out.print("Please enter a perfect number: " );
perfect = input.nextInt();
You get two numbers here. 您在这里得到两个数字。
Use a while
loop in your get
methods eg to repeatedly ask for input until a valid number is entered. 在
get
方法中使用while
循环,例如重复请求输入,直到输入有效数字为止。
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);
while(!vN ){
System.out.println("Invalid input. Try again");
count = input.nextInt();
vN = validateNum(count, perfect);
}
return count;
}
Simiilarly, update the getPerfect
method and remove int count = getNum();
同样,更新
getPerfect
方法并删除int count = getNum();
statement from this method. 此方法的声明。
EDIT: To repeatedly ask for perfect number count
times, update your main method as below: 编辑:要反复要求完美的数字
count
时间,请如下更新您的主要方法:
public static void main ( String args [] )
{
int gN = getNum();
for(int indx=0; indx <gN; indx++){
int gP = getPerfect();
//use your gP numbers in the way you want
}
}
EDIT1: To call How many numbers would you like to test? "
EDIT1:呼叫
How many numbers would you like to test? "
How many numbers would you like to test? "
tow times, I think you can simply call your getNum()
method two times in the main method as below: How many numbers would you like to test? "
两次,我想您可以在main方法中简单地两次调用getNum()
方法,如下所示:
public static void main ( String args [] )
{
int gN = getNum();//first call
gN = getNum(); //second call
int gP = getPerfect();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.