你能帮我编写这个java程序吗? 我有一个两个字符串,我需要确定哪些字符没有重复的字母。 输入是: 热爱生活 输出将是: ovif
它将删除所有重复的字符。
这是我的代码,但它只打印并查找重复的字母。
public static void main(String[] args) {
Map<Character, Integer> map = new HashMap<Character, Integer>();
input = new Scanner(System.in);
String aw, bw;
System.out.println("Input First Word: ");
aw=input.next();
System.out.println("Input Second Word: ");
bw=input.next();
String s = aw+bw;
char[] chars = s.toCharArray();
for(Character ch:chars){
if(map.containsKey(ch)){
map.put(ch, map.get(ch)+1);
} else {
map.put(ch, 1);
}
}
Set<Character> keys = map.keySet();
for(Character ch:keys){
if(map.get(ch) > 1){
System.out.println(ch+" ");
}
}
}
我想打印在该程序中删除的字符。
最佳答案
for(Character ch:keys){
if(map.get(ch) == 1){
System.out.println(ch+" ");
}
}
将打印出所有出现过一次的字符。您将两个字符串中的每个字符都添加到映射中,因此仅出现一次的字符的值为 1。
关于java - 查找字符串中所有不重复的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31563522/