首页 > 解决方案 > 条件满足时 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);

}

标签: javaandroidif-statement

解决方案


您需要使用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]);
} 

推荐阅读