Java 为什么循环没有在这里中断? 类哈希映射{ 公共静态void main(字符串参数[]){ 扫描仪s=新的扫描仪(System.in); LinkedHashMap hm=新LinkedHashMap(); while(true){ 字符串a=s.next(); 如果(a.equals(“”| | a==null){ 打破 } 否则,如果(!hm.containsKey(a)){ hm.put(a,1); } 否则{ hm.put(a,hm.get(a)+1; } } 系统输出打印项次(hm); } }

Java 为什么循环没有在这里中断? 类哈希映射{ 公共静态void main(字符串参数[]){ 扫描仪s=新的扫描仪(System.in); LinkedHashMap hm=新LinkedHashMap(); while(true){ 字符串a=s.next(); 如果(a.equals(“”| | a==null){ 打破 } 否则,如果(!hm.containsKey(a)){ hm.put(a,1); } 否则{ hm.put(a,hm.get(a)+1; } } 系统输出打印项次(hm); } },java,loops,hashmap,Java,Loops,Hashmap,当用户输入空值或字符串时,我尝试从用户获取无限值,并尝试打印hashmap中存储的值,但当我在控制台中输入空值时,循环没有中断。您需要使用s.nextline()而不是s.next(),请检查下面修改的代码: class hashmaps{ public static void main(String args[]){ Scanner s = new Scanner(System.in); LinkedHashMap<String,Integer> hm = new

当用户输入空值或字符串时,我尝试从用户获取无限值,并尝试打印hashmap中存储的值,但当我在控制台中输入空值时,循环没有中断。

您需要使用s.nextline()而不是s.next(),请检查下面修改的代码:

class hashmaps{
public static void main(String args[]){
    Scanner s = new Scanner(System.in);
    LinkedHashMap<String,Integer> hm = new LinkedHashMap<String,Integer>();
    while(true){
        String a=s.next();
        if(a.equals("") || a==null){
            break;
        }
        else if(!hm.containsKey(a)){
            hm.put(a,1);
        }
        else{
            hm.put(a,hm.get(a)+1);
        }
    }
    System.out.println(hm);
}
}
公共类哈希映射{
公共静态void main(字符串参数[]){
扫描仪s=新的扫描仪(System.in);
LinkedHashMap hm=新LinkedHashMap();
while(true){
字符串a=s.nextLine();
如果(a==null | | a.equals(“”){
打破
}
否则,如果(!hm.containsKey(a)){
hm.put(a,1);
}
否则{
hm.put(a,hm.get(a)+1;
}
}
系统输出打印项次(hm);
} }
您需要使用s.nextline()而不是s.next(),请检查下面修改的代码:

class hashmaps{
public static void main(String args[]){
    Scanner s = new Scanner(System.in);
    LinkedHashMap<String,Integer> hm = new LinkedHashMap<String,Integer>();
    while(true){
        String a=s.next();
        if(a.equals("") || a==null){
            break;
        }
        else if(!hm.containsKey(a)){
            hm.put(a,1);
        }
        else{
            hm.put(a,hm.get(a)+1);
        }
    }
    System.out.println(hm);
}
}
公共类哈希映射{
公共静态void main(字符串参数[]){
扫描仪s=新的扫描仪(System.in);
LinkedHashMap hm=新LinkedHashMap();
while(true){
字符串a=s.nextLine();
如果(a==null | | a.equals(“”){
打破
}
否则,如果(!hm.containsKey(a)){
hm.put(a,1);
}
否则{
hm.put(a,hm.get(a)+1;
}
}
系统输出打印项次(hm);
} }

您需要使用
nextLine()
而不是等待获取非空内容的
next()
,因此它在看到新行时不会停止,而不像
nextLine
。也

  • null
    检查无效
  • 您可以使用
    isEmpty
  • 使用
    merge
Scanner s=新的扫描仪(System.in);
LinkedHashMap hm=新LinkedHashMap();
while(true){
System.out.print(“给出一个词:”);
字符串a=s.nextLine();
如果(a.isEmpty()){
打破
}
hm.merge(a,1,整数::和);
}
系统输出打印项次(hm);

hm.merge(a,1,Integer::sum)意味着

  • 对于键
    a
  • 输入值
    1
  • 如果某个值已经存在,则应用
    Integer::sum
    ,与
    (prevVal,value)->prevVal+value

您需要使用
nextLine()
而不是等待获取非空内容的
next()
,因此它在看到新行时不会停止,而不像
nextLine
。也

  • null
    检查无效
  • 您可以使用
    isEmpty
  • 使用
    merge
Scanner s=新的扫描仪(System.in);
LinkedHashMap hm=新LinkedHashMap();
while(true){
System.out.print(“给出一个词:”);
字符串a=s.nextLine();
如果(a.isEmpty()){
打破
}
hm.merge(a,1,整数::和);
}
系统输出打印项次(hm);

hm.merge(a,1,Integer::sum)意味着

  • 对于键
    a
  • 输入值
    1
  • 如果某个值已经存在,则应用
    Integer::sum
    ,与
    (prevVal,value)->prevVal+value

一个主要问题是您使用的是
s.next()
而不是
s.nextLine()
:这两种方法的区别在于

查找并返回此扫描程序中的下一个完整令牌。完整标记的前面和后面是与分隔符模式匹配的输入。此方法在等待输入扫描时可能会阻塞,即使以前调用hasNext()时返回true也是如此

而不是Scanner.nextLine()

使此扫描仪前进超过当前行并返回跳过的输入。此方法返回当前行的其余部分,不包括末尾的任何行分隔符。该位置设置为下一行的开头

除此之外,当您创建扫描仪时,您必须提醒在完成后始终关闭它,因为如果您不再使用它,它会导致内存泄漏。此外,我强烈建议在需要比较两个字符串时使用
equals()
方法,但在这种特定情况下,您也可以使用更实用的
isEmpty()
。我建议你通过阅读主题和主题来深入研究这个主题。 总之,代码应该是这样的:

Scanner s = new Scanner(System.in);
LinkedHashMap<String, Integer> hm = new LinkedHashMap<>();
while (true) {
    System.out.print("Give a word: ");
    String a = s.nextLine();
    if (a.isEmpty()) {
        break;
    }
    hm.merge(a, 1, Integer::sum);
}
System.out.println(hm);

公共静态void main(字符串参数[]){
扫描仪s=新的扫描仪(System.in);
LinkedHashMap hm=新LinkedHashMap();
while(true){
字符串a=s.nextLine();
如果(a.isEmpty())
打破
否则,如果(!hm.containsKey(a))
hm.put(a,1);
其他的
hm.put(a,hm.get(a)+1;
}
s、 close();
系统输出打印项次(hm);
}

一个主要问题是您使用的是
s.next()
而不是
s.nextLine()
:这两种方法的区别在于

查找并返回此扫描程序中的下一个完整令牌。完整标记的前面和后面是与分隔符模式匹配的输入。此方法在等待输入扫描时可能会阻塞,即使以前调用hasNext()时返回true也是如此

而不是Scanner.nextLine()

使此扫描仪前进超过当前行并返回跳过的输入。此方法返回当前行的其余部分,不包括末尾的任何行分隔符。该位置设置为下一行的开头

超越

public static void main(String args[]){
        Scanner s = new Scanner(System.in);
        LinkedHashMap<String,Integer> hm = new LinkedHashMap<String,Integer>();
        while(true){
            String a=s.nextLine();
            if(a.isEmpty())
                break;
            else if(!hm.containsKey(a))
                hm.put(a,1);
            else
                hm.put(a,hm.get(a)+1);
        }
        s.close();
        System.out.println(hm);
    }