Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用于输入验证的布尔逻辑_Java_Arraylist_Boolean_Boolean Logic_Boolean Expression - Fatal编程技术网

Java 用于输入验证的布尔逻辑

Java 用于输入验证的布尔逻辑,java,arraylist,boolean,boolean-logic,boolean-expression,Java,Arraylist,Boolean,Boolean Logic,Boolean Expression,我有一个方法,它会继续循环,直到用户在设定的范围内输入一个数字。基本上,它读取arraylist上的大小,并验证用户输入是否大于-1,以及是否小于arraylist大小。然而,我似乎不能得到正确的布尔逻辑,它仍然接受错误的数字或只有一方的范围内的作品。这是密码 public void Verify() { do { System.out.printf("Enter the number\n"); while (!input.h

我有一个方法,它会继续循环,直到用户在设定的范围内输入一个数字。基本上,它读取arraylist上的大小,并验证用户输入是否大于-1,以及是否小于arraylist大小。然而,我似乎不能得到正确的布尔逻辑,它仍然接受错误的数字或只有一方的范围内的作品。这是密码

public void Verify()
{
    
    do {
        System.out.printf("Enter the number\n");
        while (!input.hasNextInt()) 
        {
            System.out.printf("Enter the number\n");
                            input.next();
        }
        choice = input.nextInt();
    } while (choice < 0 && choice >  mov.getArrayListSize());//<--- this part
    
}
public void Verify()
{
做{
System.out.printf(“输入数字”);
而(!input.hasNextInt())
{
System.out.printf(“输入数字”);
input.next();
}
choice=input.nextInt();
}而(choice<0&&choice>mov.getArrayListSize());//那么问题来了

while (choice < 0 && choice >  mov.getArrayListSize());

循环将继续,直到值落在0-len范围内,这似乎是您在这里所需要的。

只要否定整个过程,它就会正常。
while(!(choice>=0&&choice
@Amongalen刚试过,但没用。另一个suggestions@JustAnotherDeveloper仍然没有骰子…逻辑是有道理的,但现在它不接受任何数字?马修,你有没有尝试过一个新版本或你编辑成问题的版本?因为你的版本是错误的“翻译”汤姆:在编辑这篇文章之前,我先试用了他的版本,然后又试用了编辑中的版本。不幸的是,这两个版本对我都不起作用
xxxxxxxxxx
----------+---------+------------>   ... this shows a solution for choice < 0
          0         len

                     xxxxxxxxxxxx
----------+---------+------------>   ... this shows a solution for choice > len
          0         len


----------+---------+------------>   ... this shows a solution for the logical AND
          0         len
while (choice < 0 || choice >  mov.getArrayListSize());
xxxxxxxxxx           xxxxxxxxxxxx
----------+---------+------------>   ... this shows a solution for choice > len
          0         len