java - 在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") 变成真的?
解决方案
使用.equals()
而不是==
比较字符串。
推荐阅读
- sql - 是否可以防止 ORDS 逃离我的 GeoJSON?
- typescript - 如何让 testcafe-hammerhead 构建以便我可以为它做出贡献?
- python - 线程中未处理的异常由
.wrapper 在 0x7f197e469a60> - git - git:svn 不是 git 命令 - Mac
- asp.net-mvc - Select2 initSelection 元素 Val
- sql-server - 如何更改 SQL Server 系统管理员角色中用户的服务器名称/机器名称部分?
- reactjs - 如何使用 history.push() 传递变量 id
- python - Boto.exception.BotoServerError 的正文总是字符串吗?如何测试 Boto.exception.BotoServerError 400 bad request body 是否为字符串?
- angular - Angular 共享模块导出由另一个模块导入的组件
- excel - 双屏幕上的 VBA 分散用户窗体(笔记本电脑 17" 与屏幕 27")