扫描仪和循环问题

时间:2018-03-30 12:46:43

标签: java while-loop switch-statement system.in

我一直在阅读有关Scanner的问题,但即使添加了额外的in.nextLine()in.nextInt(),Java所做的就是将NoSuchElementException移到该行。有什么建议?我正在尝试为我的程序创建一个自动循环菜单选择。每次循环案例1时,都会出现问题。

while (!done) {
 System.out.println("Welcome, please type a number for selecting from the following: \n 1. Insert Process \n 2. Print out a list of processes \n 3. See and remove first priority process \n 4. Quit");

 Scanner in = new Scanner(System.in);
 int selector = 0;
 print(); in .nextLine();
 selector = in .nextInt();

 switch (selector) {
  case 1:
   System.out.println("Please enter a priority for the new process > 0");
   Scanner pin = new Scanner(System.in);
   priority = pin.nextInt();
   if (priority > 0) {
    maxHeapInsert(priority); //allows user to set priority
    in .close(); in = new Scanner(System.in);
    break;

   } else {
    System.out.println("ERROR, you did not enter a number greater than 0");
   }
   break;
 }
}

3 个答案:

答案 0 :(得分:0)

在尝试使用下一个*获取令牌之前,请使用扫描仪的hasNextLine及其他变体。

没有必要为 A B C 100 0 7 0 203 5 4 1 5992 0 10 2 2003 9 8 3 20 10 5 4 12 6 2 5 使用多个扫描仪。

答案 1 :(得分:0)

尝试以下代码。

请注意,无需使用/声明多个扫描程序。还要尽量避免在循环中声明变量,因为它没有效率(内存/性能)。

boolean done = false;
    int priority = 0, selector = 0;
    try(Scanner in=new Scanner(System.in))
    {
        while (!done ) {
            System.out.println("Welcome, please type a number for selecting from the following: \n 1. Insert Process \n 2. Print out a list of processes \n 3. See and remove first priority process \n 4. Quit");
            selector = in.nextInt();
            in.nextLine();

            switch(selector){
            case 1:
                System.out.println("Please enter a priority for the new process > 0");
                priority = in.nextInt();
                in.nextLine();

                if (priority > 0) {
                    maxHeapInsert(priority);//allows user to set priority
                    break;
                } else {
                    System.out.println("ERROR, you did not enter a number greater than 0");
                }
                break;
            }
        }
    }

答案 2 :(得分:0)

尝试以下代码:

while (!done) {
        System.out.println(
                "Welcome, please type a number for selecting from the following: \n 1. Insert Process \n 2. Print out a list of processes \n 3. See and remove first priority process \n 4. Quit");

        Scanner in = new Scanner(System.in);
        int selector = 0;
        selector = in.nextInt();

        switch (selector) {
        case 1:
            System.out.println("Please enter a priority for the new process > 0");
            Scanner pin = new Scanner(System.in);
            priority = pin.nextInt();
            if (priority > 0) {
                maxHeapInsert(priority); //allows user to set priority
                break;
            } else {
                System.out.println("ERROR, you did not enter a number greater than 0");
            }
            break;
        }
    }

原因是,当您使用in.close()时,它也会关闭System.in,因此您将读取-1并抛出异常。请记住,程序关闭后无法重新打开System.in