当我给出的输入为mama
下面的代码打印mamaa
,但我只想输出为ma
。有什么问题是代码如何使用java删除字符串中的重复字符?
import java.util.*;
class Duplicate1 {
public static void main(String args[]) {
String str;
Scanner s=new Scanner(System.in);
System.out.println("enter the string");
str=s.nextLine();
String result="";
char c=str.charAt(0);
result=result+c;
for(int i=1; i<str.length(); i++) {
char d = str.charAt(i);
for(int j=0; j<i+1; j++) {
if(d != result.charAt(j)) {
result=result+d;
}
}
}
System.out.println(result);
}
}
当我给“妈妈”输入它打印“mamaa”..但我想输出为“马”。代码有什么问题..帮助我! –
欢迎来到SO。请阅读这篇关于如何提出一个好问题的文章,然后相应地进行格式化。这样你将有更多的机会获得有用的答案。 http://stackoverflow.com/help/how-to-ask – lmazgon
请注意:不要在评论中放置更多信息,而是更新您的问题。而且请:你希望我们帮忙,你至少需要3分钟**才能正确**格式化你的问题! – GhostCat