带整数验证的Java扫描器

带整数验证的Java扫描器,java,Java,如果整数满足某些条件并分配给arrayList,那么从扫描程序中获取整数的正确语法是什么?它应该循环并获取输入,直到我们插入“-1”。 它停止,然后我插入“q”,但我需要它来处理某个整数 Scanner sc = new Scanner(System.in); while (-99 !== sc.nextLine() && sc.hasNextInt()) { //d

如果整数满足某些条件并分配给arrayList,那么从扫描程序中获取整数的正确语法是什么?它应该循环并获取输入,直到我们插入“-1”。 它停止,然后我插入“q”,但我需要它来处理某个整数

              Scanner sc = new Scanner(System.in);
              while (-99 !== sc.nextLine() && sc.hasNextInt()) 
              {
              //do something Whi
              }

我不确定你的具体要求是什么,但是你的要求呢

Scanner sc = new Scanner(System.in);
while (true) 
{
    int numEntered = sc.nextInt ();
    if (numEntered  == -1) {
        break;
    }
    // do something
}

您可能想这样做:

Scanner sc = new Scanner(System.in);
int num;
do{
    while (!sc.hasNextInt()){
        sc.next();
    }
    num = sc.nextInt();
}while(num!=-1)

希望这有帮助

这将循环直到遇到-1,并将元素添加到arraylist。 如果你在这方面需要帮助,请告诉我

List<Integer> list = new ArrayList<>();
Scanner scanner = new Scanner(System.in);

// keep looping until -1 is encountered
while (true) {
        int temp = scanner.nextInt();
        if (temp == -1) {
            break;
        }
        list.add(temp);
}

// Printing the array list
System.out.println(list);

// closing the scanner is important
scanner.close();
List List=new ArrayList();
扫描仪=新的扫描仪(System.in);
//保持循环直到遇到-1
while(true){
int temp=scanner.nextInt();
如果(温度==-1){
打破
}
列表。添加(临时);
}
//打印数组列表
系统输出打印项次(列表);
//关闭扫描仪很重要
scanner.close();

sc如果引用的是Scanner类的对象而不是int对象。您必须检查while循环中next int的值

扫描仪对象如何等于负数
int
?来自扫描仪的输入应该是正确的,请告诉我如何正确操作谢谢!我明白我做错了什么!这对我有用!