无法打印scan.nextIn();的值

时间:2019-01-17 00:32:56

标签: java arrays if-statement while-loop java.util.scanner

我正在尝试从数组中删除用户整数输入。但是,我无法获得输入的值。

while(keepGoing)
    {
        while (!scan.hasNextInt() ) 
        {        
            scan.next(); 
            System.out.print('\n'+"Choose a valid number: "); 
        }
        int unitA = scan.nextInt();
        if (unitA < 1) 
        {
            System.out.print('\n'+"Choose one of the options: ");
            keepGoing = true;
        }
        else if (unitA > 14) 
        {
            System.out.print('\n'+"Choose one of the options: ");
            keepGoing = true;
        }
        else
            lengthValue.remove(unitA);
        scan.close();
        keepGoing = false;
    }
    //lengthValue.remove(int unitA);
    System.out.println(unitA);

1 个答案:

答案 0 :(得分:0)

我认为,您忘记输入输入后按“ Enter”。仅当您按“ Enter”键时,扫描仪才能读取输入。您的解决方案对我来说似乎是正确的。我能够在PC上正确运行它。

您可以在此处找到类似的问题: How to use Scanner to accept only valid int as input