首页 > 解决方案 > 在字符串中查找重复的字符 - 但没有得到预期的结果 - 作为一个尝试学习的初学者 - 请有人帮我解决

问题描述

谁能帮助我以下程序中的错误是什么。实际我试图找到重复的字符,如果它重复布尔重复应该是真的。但是当我尝试调试时,它没有找到重复项。

void main() {
    String word = "abcda";
    for (int i = 0; i < word.length(); i++) {
        char ch = word.charAt(i);
        boolean repeat1 = false;
        for (int j = i + 1; j < word.length(); j++) {
            if (ch == word.charAt(j)) {
            System.out.println("repeated " + ch);
            repeat1 = true;
            break;
        }  
        if(repeat1==false) {
            System.out.println("Not repeated " + ch);
            break;
        }
    }
}

实际结果:

    Not repeated a
    Not repeated b
    Not repeated c
    Not repeated d

预期结果:我应该得到结果

    repeated a
    Not repeated b
    Not repeated c
    Not repeated d

标签: java

解决方案


推荐阅读