Java if-else比较
我有一个程序测试,看看用户输入的是正数还是整数。Java if-else比较,java,Java,我有一个程序测试,看看用户输入的是正数还是整数。if语句测试它是否为整数,else if语句测试它是否为负值。如果是负数或小数,则要求用户输入正整数。问题在于else语句,它正在等待用户再次输入,我希望它使用System.out.print(“输入测试编号:”)中的值是否通过if和else if测试 我尝试在System.out.println(“请输入一个整数”)之后分配用户输入转换为int变量,但如果用户输入一个double,我会得到一个错误,所以我认为这种方法不起作用。任何关于如何使程序工
if
语句测试它是否为整数,else if
语句测试它是否为负值。如果是负数或小数,则要求用户输入正整数。问题在于else语句,它正在等待用户再次输入,我希望它使用System.out.print(“输入测试编号:”)中的值代码>是否通过if和else if测试
我尝试在System.out.println(“请输入一个整数”)之后分配用户输入
转换为int变量,但如果用户输入一个double,我会得到一个错误,所以我认为这种方法不起作用。任何关于如何使程序工作的见解都将受到赞赏,谢谢
import java.util.Scanner;
public class FibonacciNumbersTester
{
public static void main(String[]args)
{ //Variables
Scanner userDed = new Scanner(System.in);
String userChoice = "Y";
while(userChoice.equalsIgnoreCase("Y"))
{
Scanner userNum = new Scanner(System.in);
System.out.print("Enter the test number: ");
if(!userNum.hasNextInt() )
{
System.out.println("Please enter an integer!");
}
else if(userNum.nextInt() < 0 )
{
System.out.println("Please enter a postive integer!");
}
else
{
int NumTo = userNum.nextInt();
System.out.println(NumTo);
}
System.out.print("Would you like to continue? (Y/N)");
userChoice = userDed.next();
}
}
}
import java.util.Scanner;
公共类FibonacciNumbersTester
{
公共静态void main(字符串[]args)
{//变量
扫描仪用户=新扫描仪(System.in);
字符串userChoice=“Y”;
while(userChoice.equalsIgnoreCase(“Y”))
{
Scanner userNum=新扫描仪(System.in);
系统输出打印(“输入测试编号:”);
如果(!userNum.hasNextInt())
{
System.out.println(“请输入一个整数!”);
}
else if(userNum.nextInt()<0)
{
System.out.println(“请输入一个正整数!”);
}
其他的
{
int NumTo=userNum.nextInt();
系统输出println(NumTo);
}
系统输出打印(“您想继续吗?(Y/N)”);
userChoice=userDed.next();
}
}
}
谢谢。您应该打一次电话,保存结果并将其用于比较
试试这个:
public static void main(String[] args) { // Variables
Scanner userDed = new Scanner(System.in);
String userChoice = "Y";
while (userChoice.equalsIgnoreCase("Y")) {
Scanner userNum = new Scanner(System.in);
System.out.print("Enter the test number: ");
if (!userNum.hasNextInt()) {
System.out.println("Please enter an integer!");
} else {
int NumTo = userNum.nextInt();
if (NumTo < 0)
System.out.println("Please enter a postive integer!");
else
System.out.println(NumTo);
}
System.out.print("Would you like to continue? (Y/N)");
userChoice = userDed.next();
}
}
publicstaticvoidmain(String[]args){//变量
扫描仪用户=新扫描仪(System.in);
字符串userChoice=“Y”;
while(userChoice.equalsIgnoreCase(“Y”)){
Scanner userNum=新扫描仪(System.in);
系统输出打印(“输入测试编号:”);
如果(!userNum.hasNextInt()){
System.out.println(“请输入一个整数!”);
}否则{
int NumTo=userNum.nextInt();
如果(NumTo<0)
System.out.println(“请输入一个正整数!”);
其他的
系统输出println(NumTo);
}
系统输出打印(“您想继续吗?(Y/N)”);
userChoice=userDed.next();
}
}
将userNum.nextInt()赋值给变量,如int x=userNum.nextInt(),并在else if(x<0)和else中使用x。我是否将其赋值给if语句之前的变量?如果是,如果用户以双精度输入,这不会给我一个错误吗?谢谢Wanto,非常感谢你!真不敢相信我居然没想到这个!在过去的8个小时里,我一直在改变我的计划,你在这里救了我的命。谢谢你!
Pattern positiveInt = Pattern.compile("^[1-9]\d*$"); // for positive integer
if(!userNum.hasNext(positiveInt)) {
System.out.println("Please enter an positive integer (greater than 0) !");
}
else {
int NumTo = userNum.nextInt(positiveInt);
System.out.println(NumTo);
}