Do-while循环不等待用户输入?

时间:2016-03-31 03:46:59

标签: java loops do-while

我确信这很简单,我无法发现,我有一个while循环提示用户输入数组大小,这将用于程序的其余部分。如果用户输入正确的输入,程序将继续并正常工作,但如果用户输入错误的输入...

public static void main(String[] args)
{
  // user enters up to 20 double values, stored in an array, user should enter 99999 to quit entering numbers. If user has not entered any numbers yet
  // display an error message, otherwise, display each entered value and it's distance from the average

  Scanner keyboard = new Scanner(System.in);
  int arraySize = 0;
  boolean isValid = false;

  do
  {
     isValid = true;
     arraySize = 0; // reset these values at start of each loop.

     System.out.println("Enter an array size.");
     try {
        arraySize = keyboard.nextInt();
     }
     catch(NegativeArraySizeException mistake) {
        System.out.println("Do not enter a negative number for the arrays size.");
        System.out.println();
        isValid = false;
     }
     catch(InputMismatchException mistake) {
        System.out.println("Make sure to enter a valid number.");
        System.out.println();
        isValid = false;
     }
  } while (isValid == false);

如果用户输入无效输入,例如“红色”,则捕获块将启动并打印“确保输入有效数字”。和“输入数组大小。”一遍又一遍,不给用户实际输入任何输入的机会。我想重置arraySize变量会修复它,但事实并非如此。我猜键盘缓冲区中有东西,但到目前为止还没有空打印的组合。

我听说不应该使用例外来验证用户输入。那是为什么?

无论如何,这与此问题无关,因为它是异常处理中的练习。

5 个答案:

答案 0 :(得分:2)

不使用isValid布尔变量并为输入创建简单代码。

int arraySize = 0;
do {
    System.out.println("Enter a valid array size.");
    try {
        arraySize = Integer.valueOf(keyboard.nextLine());
        if (arraySize < 0) throw new NegativeArraySizeException();// for negative arry size
        break;// loop break when got a valid input
    } catch (Exception mistake) {
        System.err.println("Invalid input: " + mistake);
    }
} while (true);

答案 1 :(得分:1)

您可以添加keyboard.nextLine();如果发生异常,它应该解决问题。

 try {
    arraySize = keyboard.nextInt();
 }
 catch(NegativeArraySizeException mistake) {
    System.out.println("Do not enter a negative number for the arrays size.");
    System.out.println();
    isValid = false;
    keyboard.nextLine();
 }
 catch(Exception mistake) {
    System.out.println("Make sure to enter a valid number.");
    System.out.println();
    isValid = false;
    keyboard.nextLine();
   }

答案 2 :(得分:0)

请查看此修复程序是否适合您。当您尝试从nextInt函数获取字符串时,扫描程序有问题。在这里我已经获取了字符串并解析为Integer,然后处理了数字格式异常

public static void main(String[] args) {
    // user enters up to 20 double values, stored in an array, user should enter 99999 to quit entering numbers. If user has not entered any numbers yet
    // display an error message, otherwise, display each entered value and it's distance from the average

    Scanner keyboard = new Scanner(System.in);
    int arraySize = 0;
    boolean isValid = false;

    do {
        isValid = true;
        arraySize = 0; // reset these values at start of each loop.

        System.out.println("Enter an array size.");
        try {
            arraySize = Integer.parseInt(keyboard.next());
        } catch (NegativeArraySizeException mistake) {
            System.out.println("Do not enter a negative number for the arrays size.");
            System.out.println();
            isValid = false;
        } catch (InputMismatchException mistake) {
            System.out.println("Make sure to enter a valid number.");
            System.out.println();
            isValid = false;
        } catch (NumberFormatException nfe) {
            System.out.println("Make sure to enter a valid number.");
            System.out.println();
            isValid = false;
        }

    } while (isValid == false);
}

答案 3 :(得分:0)

mmuzahid几乎就在那里。但我还添加了一种检查负数的方法。试试这个

    Scanner keyboard = new Scanner(System.in);
    int arraySize = 0;
    boolean isValid = false;
    do {
        System.out.println("Enter a valid array size.");
        try {
            arraySize = Integer.valueOf(keyboard.nextLine());

            if (arraySize < 0) {
                System.out.println("Make sure to enter a valid positive number.");
            } else {
                break;
            }
        } catch (Exception mistake) {
            System.out.println("Make sure to enter a valid number. Error:" + mistake);
        }
    } while (true);

答案 4 :(得分:0)

使用keyboard.nextLine()NumberFormatException

do {
            // more code
            try {
                arraySize = Integer.valueOf((keyboard.nextLine()));
            } catch (NegativeArraySizeException mistake) {
                // more code
                isValid = false;
            } catch (InputMismatchException mistake) {
                // more code
                isValid = false;
            } catch (NumberFormatException mistake) {
                // more code
                isValid = false;
            }
} while (isValid == false);