首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

输入一个字符串 删除串中所有相同的字符,该怎么处理

2012-01-03 
输入一个字符串删除串中所有相同的字符importjava.io.*publicclassZo{publicstaticvoidmain(Stringarg[])

输入一个字符串 删除串中所有相同的字符
import   java.io.*;
public   class   Zo{
public   static   void   main(String   arg[])   throws   IOException   {
InputStreamReader   str=new   InputStreamReader(System.in);
BufferedReader   str1=new   BufferedReader(str);
String   s=str1.readLine();
String   ch= " ";

  for(int   i=s.length();i> =0;i--){
  int   j=0;
  while(   j <i){
  if(s.charAt(i)==s.charAt(j))
  break;
  else   j++;
  }
                  System.out.println(s.charAt(i));
   
    }
     
}
}


为什么我这样写不能输出啊!

[解决办法]
import java.io.*;
public class Zo{
public static void main(String arg[]) throws IOException {
public static void main(String arg[]) throws IOException {
InputStreamReader str=new InputStreamReader(System.in);
BufferedReader str1=new BufferedReader(str);
String s=str1.readLine();

for (int i = 0; i < s.length(); i++) {
s = s.substring(0, i + 1) + s.substring(i + 1).replaceAll( " "+s.charAt(i), " ");
}
System.out.println(s);
}
}

热点排行