Java 检查字符串中是否有重复的字母,无论大小写 公共静态布尔检查(字符串输入){ Set tmp=new HashSet(); for(char ch:input.toCharArray()){ if(Character.isleter(ch)和&!tmp.add(ch)){ 返回true; } } 返回false; }

Java 检查字符串中是否有重复的字母,无论大小写 公共静态布尔检查(字符串输入){ Set tmp=new HashSet(); for(char ch:input.toCharArray()){ if(Character.isleter(ch)和&!tmp.add(ch)){ 返回true; } } 返回false; },java,arrays,for-loop,if-statement,Java,Arrays,For Loop,If Statement,我对编程非常陌生,我找到了一种在字符串中查找重复字母的方法,但是如果重复字母是大写或非大写字母,例如“Aa”,我如何才能返回true。在for循环之前将输入更改为小写 public static boolean check(String input) { Set<Character> tmp = new HashSet<Character>(); for(char ch : input.toCharArray()) { if (Charac

我对编程非常陌生,我找到了一种在字符串中查找重复字母的方法,但是如果重复字母是大写或非大写字母,例如“Aa”,我如何才能返回true。

在for循环之前将
输入更改为小写

public static boolean check(String input) {
    Set<Character> tmp = new HashSet<Character>();
    for(char ch : input.toCharArray()) {
        if (Character.isLetter(ch) && !tmp.add(ch)) {
            return true;
        }
    }
    return false;
}
公共静态布尔检查(字符串输入){
Set tmp=new HashSet();
//这将使所有大写字母都小写,但不会更改原始字符串
String testInput=input.toLowerCase();
for(char ch:testInput.toCharArray()){
if(Character.isleter(ch)和&!tmp.add(ch)){
返回true;
}
}
返回false;
}

很抱歉,我不明白你的意思。@aranrasull-我已经更新了答案以包含代码。如果你需要解释为什么这样做,请告诉我。
public static boolean check(String input) {
    Set<Character> tmp = new HashSet<Character>();
    // This will make all capital letters lowercase but will not change the original string
    String testInput = input.toLowerCase();
    for(char ch : testInput.toCharArray()) {
        if (Character.isLetter(ch) && !tmp.add(ch)) {
            return true;
        }
    }
    return false;
}