首页 > 解决方案 > 在java中使用扫描仪的麻烦

问题描述

扫描仪类不能按我的预期工作。

Scanner in = new Scanner(System.in); // input: total

String str = in.nextLine();          // total
System.out.println(str);             // total
System.out.println(str == "total");  // false
System.out.println(str != "total");  // true

我预计第 5 行是真的,但上面的代码就是我得到的。

我应该怎么做 for (str == "total") 变成真的?

标签: java

解决方案


使用.equals()而不是==比较字符串。


推荐阅读