當我給出的輸入爲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