public class sss {
   static  char[] cass ={'a','b','c','d','a'};
    public static void main(String[] args)
    {
       int a=0;
       String sddd = new String(cass);
       for(int ad=1;ad<=3;ad++)
           if(cass[0]==cass[ad])
           {cass[ad]='@';}
       for(int aa=0;aa<=4;aa++)
       {System.out.print(cass[aa]);}
    }      
}
输出为abcda
我想让他输出为abcd@ 怎么做啊?

解决方案 »

  1.   

    范围搞错了
    for(int ad=1;ad<=3;ad++)
               if(cass[0]==cass[ad])
               {cass[ad]='@';}这里应该是 <=4 吧
      

  2.   

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.Reader;
    import java.sql.*; 
    import java.sql.ResultSet;   
    public class Zhuanyouci 
    {
        File newFile;
        FileReader fis;
        Reader in;
        static char[] cha;
        private String str1;
        private String str2;
        private String[] str3;
        private String[] str4;
        public Zhuanyouci() 
        {
           newFile = new File("duanju.txt");
           cha = new char[(int)newFile.length()];
           str3 = new String[1000];
           str4 = new String[1000];
           //Connection con = null;
           //Statement stmt = null;
           //ResultSet rs = null;
           //String strurl="jdbc:odbc:MS Access Database2";
           //String user="MS Access Database2";
           //String password=" "; 
           try 
           {
                fis = new FileReader(newFile); 
                in = new BufferedReader(fis);
                in.read(cha); 
               //System.out.print(cha);
           /*try
            {
                //指明你加载的mdb文件的位置               
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //加载驱动系统自带的   
                con = DriverManager.getConnection(strurl,user,password); //获得连接    
                stmt=con.createStatement();  //创建Statement语句对象,用于执行各种的SQL数据库处理。
                rs=stmt.executeQuery("select * from xinci"); //执行SQL语句;   
                try{
                    while(rs.next())   
                    {  
                        
                    }   
                     }catch(Exception ex)
                    {
                     System.out.println("没连接成功");
                     }
                    }
                     catch(Exception ex)
                   {
                System.out.println("哈哈");//输出错误信息。
            }
            try{
                if(rs!=null)
             {
                 rs.close();
              }
              if(stmt!=null)
              {
                  stmt.close();
              }
              if(con!=null)
              {
                   con.close();
               } 
           }catch(SQLException sqle){}*/
           int i=0,ss=0,ad=0,ac=0; 
           char a;
           System.out.println("到这没");
           while(i<=(int)newFile.length()/2-2)
           {
              if((int)cha[i]<=120||cha[i]=='的'||cha[i]=='了'||cha[i]=='是'||cha[i]=='之'||cha[i]=='不')
              {
                  i=i+1;
              }
              else 
               {
                   int j=i+1;
                   while(j<=(int)newFile.length()/2-2)
                   {
                       if((int)cha[j]<=120||cha[j]=='的'||cha[j]=='了'||cha[j]=='是'||cha[j]=='之'||cha[j]=='不')
                       {
                           j++;
                       }
                       else
                       {
                            if(cha[i]==cha[j])
                            {                
                                 if(cha[i+1]==cha[j+1]&&cha[i+2]==cha[j+2]&&(int)cha[j+2]>120)
                                 {
                                     char[] ch1 = {cha[i],cha[i+1],cha[i+2]};                          
                                     str1 = new String(ch1);
                                     str3[ad++]=str1;
                                     System.out.println(str1);
                                     cha[j]='@';cha[j+1]='@';cha[j+2]='@';
                                     ss=2;
                                     break;
                                  }
                                 else if(cha[i+1]==cha[j+1]&&(int)cha[j+1]>120)
                                {
                                     char[] ch2 = {cha[i],cha[i+1]};                          
                                     str2 = new String(ch2);
                                     System.out.println(str2);
                                     str4[ac++]=str2; 
                                     cha[j]='@';cha[j+1]='@';
                                     ss=1;
                                     break;                                
                                }
                             }                           
                            j++;                 
                       }
                    }
                    switch(ss)
                    {
                        case 1: i=i+2;break;
                        case 2: i=i+3;break;
                     }
                }
             }
           for(int chu=0;chu<=(int)newFile.length()/2-2;chu++)
           {
               System.out.print(cha[chu]);
           }
             /*for(int ch=0;ch<=ad;ch++)
            {
               if((int)str3[ch].charAt(1)<=120)
               {
                   ch++;
               }
               else if(str3[ch].charAt(2)=='的'||str3[ch].charAt(2)=='了'||str3[ch].charAt(2)=='是'||str3[ch].charAt(2)=='之'||str3[ch].charAt(2)=='不')
               {
                   ch++;
               }
               else
               {
                        int qq=0;
                        while(qq<=ad)
                        {     
                             if(str3[ch].equals(str3[qq]))
                             {
                               str3[qq]=str3[qq+1];
                            }
                             else
                                 qq++;
                        }   
               }
                for(int cc=0;ch<=ac;cc++)
              {
                    if((int)str4[cc].charAt(1)<=120)
                    {
                           cc++;
                    }
                     else if(str4[cc].charAt(1)=='的'||str4[cc].charAt(1)=='了'||str4[cc].charAt(1)=='是'||str4[cc].charAt(1)=='之'||str4[cc].charAt(1)=='不')
                     {
                           cc++;
                     }
                     else
                    {
                           int qq=0;
                           while(qq<=ac)
                        {     
                             if(str4[cc].equals(str4[qq]))
                             {
                               str4[qq]=str4[qq+1];
                            }
                             else
                                 qq++;
                        }   
                    }
                }
           }*/
            in.close(); 
            fis.close();
            } catch (FileNotFoundException ex) 
           {
                ex.printStackTrace();
           }  catch (IOException ex) 
            {
                ex.printStackTrace();
            } 
           int i1=0,i2=0;
           while(str3[i1]!=null)
           {System.out.println(str3[i1]);
               i1++;}
           while(str4[i2]!=null)
           { System.out.println(str4[i2]);
               i2++;}
        
        }
       public static void main(String[] args)
       {
           new Zhuanyouci();
       }}
    帮忙运行以下这个程序,有点慢 ,duanju.txt是去了标点的文本.有点慢 不知道如何改进了,请高手帮帮忙,弄了好几天了也不好用.在做<红楼梦>