繁体   English   中英

Java while循环外的扫描器

[英]Scanner outside Java while loop

我正在创建一种方法,让用户通过与数字相关的 2 个选择来选择他想做的事情。 如果用户在输入中插入任何字符串,我的代码将无限打印:

Choose an optionError
1 - New game
2 - Load game

在所有其他情况下代码工作正常,所以我认为错误在 catch() 中。 我尝试使用代码某些部分中的说明关闭扫描仪 object,但问题仍然存在。

相反,如果我在 Start() 方法的 while 循环内声明 Scanner object,代码将完美运行。 我不知道扫描仪 object 是如何工作的,也不知道为什么会出现这个问题。

import java.util.Scanner;

public class Metods {

    static Scanner input = new Scanner(System.in);

    public static int Start() {
        while(true) {
            
            int choice;
    
            System.out.println("1 - New game");
            System.out.println("2 - Load game");
            System.out.print("\nChoose an option");
    
            try {
                choice = input.nextInt();
                //input.close();
            } catch (Exception e) {
                System.out.println("Error");
                //input.close();
                continue;
            }

            if (choice == 1 || choice == 2) {
                //input.close();
                return choice;
            }
            else {
                System.out.println("Error");
                //input.close();
            }
        }
    }


}```

在每个循环中,您都试图再次解析输入。 nextInt() 方法抛出异常,但输入仍未处理,因此下一个循环再次尝试解析输入...

您应该将输入读取为字符串,检查它是否是有效选项(1 或 2)并将选项作为 integer 返回(如果需要),否则循环将再次开始等待新输入,因为您的输入已处理。

我只是更改了相关部分。

 public static int start() {
        while(true) {

            String choice;

            System.out.println("1 - New game");
            System.out.println("2 - Load game");
            System.out.print("\nChoose an option");

            try {
                choice = input.next();
                //input.close();
            } catch (Exception e) {
                System.out.println("Error");
                //input.close();
                continue;
            }

            if (choice.equals("1") || choice.equals("2")){
                //input.close();
                return Integer.parseInt(choice);
            }
            else {
                System.out.println("Error");
                //input.close();
            }
        }
    }

因为nextInt读到的数据不是一个integer,但是并没有自动跳过数据。

您可以使用 input.next() 方法跳过错误的字符

    static Scanner input = new Scanner(System.in);

    public static int Start() {
        while(true) {
            int choice;
    
            System.out.println("1 - New game");
            System.out.println("2 - Load game");
            System.out.print("\nChoose an option");
    
            try {
                choice = input.nextInt();
                //input.close();
            } catch (Exception e) {
                System.out.println("Error");
                if( input.hasNext() ) {
                    input.next();
                }
                //input.close();
                continue;
            }

            if (choice == 1 || choice == 2) {
                //input.close();
                return choice;
            }
            else {
                System.out.println("Error");
                //input.close();
            }
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM