Java 如何使用循环排除负整数?

Java 如何使用循环排除负整数?,java,Java,所以我试图制作一个只接受正int值的程序 我插入的while循环成功循环,如果我输入了一个非整数,直到我输入了一个有效的整数 然而,我在做同样的事情使它只接受正整数时遇到了麻烦。当我输入一个非整数时,它会再次成功地询问我,但当我输入一个neg int时,它似乎跳过了if语句并继续执行程序。为什么会这样 在我输入一个正整数之前,有没有关于如何使其循环的提示 while(!scnr.hasNextInt()){ System.out.print("Please enter

所以我试图制作一个只接受正int值的程序

我插入的while循环成功循环,如果我输入了一个非整数,直到我输入了一个有效的整数

然而,我在做同样的事情使它只接受正整数时遇到了麻烦。当我输入一个非整数时,它会再次成功地询问我,但当我输入一个neg int时,它似乎跳过了if语句并继续执行程序。为什么会这样

在我输入一个正整数之前,有没有关于如何使其循环的提示

while(!scnr.hasNextInt()){

            System.out.print("Please enter an integer value as decimal digits: ");
            if(dmCyl < 0){
                System.out.print("Please enter a positive integer value: ");
            }
            scnr.next();
            }

        dmCyl = scnr.nextInt();
while(!scnr.hasNextInt()){
System.out.print(“请输入一个整数值作为十进制数字:”);
如果(dmCyl<0){
System.out.print(“请输入正整数值:”);
}
scnr.next();
}
dmCyl=scnr.nextInt();
我也尝试过在非整数的while循环的下方和外部使用类似的while循环,但没有效果

while(dmCyl < 0){

     System.out.print("Please input positive integer: ");

     scnr.next();
}
while(dmCyl<0){
系统输出打印(“请输入正整数:”);
scnr.next();
}

您可以使用以下内容:

    System.out.println("Enter a positive integer: ");
    Scanner sc = new Scanner(System.in);
    String input = sc.nextLine(); //take the input in string format
    boolean valid=false;
    boolean test=true;
    int testnumber=0;

    while(valid==false)
    {
        /*Check for non-integers*/
        try
        {
            testnumber=Integer.parseInt(input); //this can take any value
        }
        catch(NumberFormatException ex){ //to check if the input is an integer or not
            System.out.println("Please enter a number: ");
            input = sc.nextLine();
            valid=false; //still cant get out of the while loop
            test=false; //this will always be false in case of non-integers
        }
        /*Check for positive integers*/
        if(test==true)//integer has been entered
        {
            if(testnumber<0)
            {
                System.out.println("please enter a positive integer: ");
                input = sc.nextLine();
            }
            else
            {
                valid =true; //positive integer entered, get out of the while loop
            }
        }
        test=true;
    }
    System.out.println("You entered: "+testnumber);
System.out.println(“输入一个正整数:”);
扫描仪sc=新的扫描仪(System.in);
字符串输入=sc.nextLine()//以字符串格式获取输入
布尔有效=假;
布尔检验=真;
int testnumber=0;
while(valid==false)
{
/*检查非整数*/
尝试
{
testnumber=Integer.parseInt(输入);//可以接受任何值
}
catch(NumberFormatException ex){//检查输入是否为整数
System.out.println(“请输入一个数字:”);
输入=sc.nextLine();
valid=false;//仍然无法退出while循环
test=false;//如果是非整数,则始终为false
}
/*检查是否有正整数*/
如果(test==true)//已输入整数
{

如果一个或两个小时前有人问过类似的问题。也是你吗?还是同学?也许同学,我找不到类似的问题,你能把它联系起来吗
    System.out.println("Enter a positive integer: ");
    Scanner sc = new Scanner(System.in);
    int input = sc.nextInt(); //take the input in string format
    while(input<0)
    {
        System.out.println("Enter a positive integer: ");
        input=sc.nextInt();
    }
    System.out.println("you entered: "+input);