java - 条件满足时 if 语句不打印正文
问题描述
我希望输出将是 wordList 中的字符串,当它在 for 循环退出之前与搜索匹配时,但它不会在每次 if 语句满足条件时打印出来。
搜索 = "ABC"
wordList = [["ABC", "123"], ["ABC", "456"], ["DEF", "123"]]
public void biDi(String searchWord, String[][] wordList) {
int start = 0;
int end = list.size ()-1;
String search = searchWord;
int path = 0;
for (int i = 0; (i < (list.size ()/2)); i++) {
if (search == wordList[start][0]) {
System.out.println (wordList[start][1]);
}
if (search == wordList[end][0]) {
System.out.println (wordList[end][1]);
}
start++;
end--;
path++;
}
System.out.println (path);
}
解决方案
您需要使用equals
而不是==
,使用==
字符串比较引用,而不是值。
if (search.equals(wordList[start][0])) {
System.out.println(wordList[start][1]);
}
if (search.equals(wordList[end][0])) {
System.out.println(wordList[end][1]);
}
推荐阅读
- javascript - 如何在wordpress的前端使用ajax保存帖子和自定义字段值?
- google-chrome-extension - chrome.storage.sync 限制与 Google Keep
- ios - 如何在 iOS 中转换时区格式?
- sql - 在使用 SQL Server 进行增量加载时在单独的表中捕获更新和删除记录
- tabulator - 函数 table.setColumnLayout(columnSettings); 不适合我
- powershell - 如何读取具有两个数据集(即两个不同的标题和列)的 CSV 文件
- flutter - Flutter SizedBox 与 LimitedBox
- flutter - 如何更改 Flutter 中子树的文本颜色?
- javascript - 我究竟做错了什么?类型错误:这是未定义的
- python - Python 3 的帮助功能中的西里尔字母在 Powershell Windows 10 中不起作用