【问题标题】:Why is scanner reading first before the output?为什么扫描仪在输出之前先读取?
【发布时间】:2020-02-02 22:44:54
【问题描述】:

我正在用 Hackerrank 做地图练习。目标是输入要输入的条目总数,然后输入键和值,直到达到条目数。还要寻找密钥是否存在。但是,我的代码运行不正常。即使我在扫描仪输入之前打印出“请输入姓名以查找电话号码:”,但扫描仪输入首先读取。这是为什么呢?

Scanner scanner = new Scanner(System.in);
Map<String, Integer> phone = new HashMap<>();
System.out.print("Please enter total number of entries: ");
int n = scanner.nextInt();
scanner.nextLine();

for(int x=0;x<n;x++) {
    System.out.print("Please enter name: ");
    String name = scanner.nextLine();
    System.out.print("Please enter phone number: ");
    int num = scanner.nextInt();
    scanner.nextLine();
    phone.put(name,num);
}

while(scanner.hasNext()) {
    System.out.println("Please enter name to find phone number: "); //the problem is here
    String findName = scanner.nextLine(); // this will read first before above line
    if(phone.containsKey(findName)) {
        System.out.println(findName + "=" + phone.get(findName));
    } else {
        System.out.println("not found");
    }
}

【问题讨论】:

    标签: java dictionary java.util.scanner


    【解决方案1】:

    问题出在 while 循环上——直到 Scanner 检测到输入可用时循环才会开始,因此程序会阻塞等待输入。或许可以暂时做一下,这样就可以开始了。

    你仍然需要一些机制来让你退出最后一个循环,也许检查输入的字符串是否有"exit" 或类似的字符串

    事实上,我会完全摆脱scanner.hasNext(),因为它真的没有任何用处,因为scanner.nextLine() 会为你做必要的阻止。

    相反,我会创建一个标记值,例如:

    private static final String SENTINEL = "exit";
    

    然后循环直到输入该值:

    String findName = "";
    while (!findName.equalsIgnoreCase(SENTINEL)) {
        System.out.print("Please enter name to find phone number or \"" + SENTINEL + "\" to exit: " );
        findName = scanner.nextLine();
    
        if (phone.containsKey(findName)) {
            System.out.println(findName + "=" + phone.get(findName));
        } else if (!findName.equalsIgnoreCase(SENTINEL)){
            System.out.println("not found");
        }
    } 
    

    例如:

    import java.util.*;
    
    public class Foo {
        private static final String SENTINEL = "exit";
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            Map<String, Integer> phone = new HashMap<>();
            System.out.print("Please enter total number of entries: ");
            int n = scanner.nextInt();
            scanner.nextLine();
    
            for (int x = 0; x < n; x++) {
                System.out.print("Please enter name: ");
                String name = scanner.nextLine();
                System.out.print("Please enter phone number: ");
                int num = scanner.nextInt();
                scanner.nextLine();
                phone.put(name, num);
            }
    
            String findName = "";
            while (!findName.equalsIgnoreCase(SENTINEL)) {
                System.out.print("Please enter name to find phone number or \"" + SENTINEL + "\" to exit: " );
                findName = scanner.nextLine();
                if (phone.containsKey(findName)) {
                    System.out.println(findName + "=" + phone.get(findName));
                } else if (!findName.equalsIgnoreCase(SENTINEL)){
                    System.out.println("not found");
                }
            } 
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-12-22
      • 2022-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多