当前位置: 代码迷 >> J2SE >> ,陷入死循环了,统计字符串中每个字母的个数
  详细解决方案

,陷入死循环了,统计字符串中每个字母的个数

热度:7462   发布时间:2013-02-25 00:00:00.0
求助,陷入死循环了,统计字符串中每个字母的个数
cope函数是把string对象字符串去掉空格,然后返回string对象
str是输入的字符串对象



//计算每个字母出现的次数

String str1=cope(str);
int n=1;
do{
//从字符串的第二个数开始比较,计算地一个字符出现的次数
for(int j=1;j<str1.length();j++){
if(str1.charAt(j)==str1.charAt(0))
n++;
}
//将统计过的字母全部转化为空格
str1=str1.replace(str1.charAt(0),' ');
//输出统计字母出现的总次数
  System.out.println("字母"+str1.charAt(0)+"的个数为:"+n);
//调用cope函数将str1字符串空格去掉
str1=cope(str1);
  //n重置为1
  n=1;
}while(str1.length()!=0);

------解决方案--------------------------------------------------------
Java code
没有可用分,求解决啊import java.io.*;import java.net.*;import java.awt.*;import java.awt.event.*;class liaot extends Frame{    Socket socket=null;    private TextArea text1;    private TextField text2;//图形界面    liaot(){        text1 = new TextArea();        text2 = new TextField();        add(text1,"Center");        add(text2,"South");        event();        setBounds(300,300,300,300);        setVisible(true);    }//事件响应    public void event(){        addWindowListener(new WindowAdapter(){            public void windowClosing(WindowEvent e){                System.exit(0);            }        });        text2.addActionListener(new ActionListener(){            public void actionPerformed(ActionEvent e){                try{                    socket = new Socket("192.168.30.129",3334);                }catch(Exception ee){}                new Thread(new clientsend()).start();                new Thread(new clientrec()).start();                text2.setText("");            }        });    }    public static void main(String[] args)throws Exception     {        new liaot();    }//发送    class clientsend implements Runnable    {        public void run(){            byte[]buf = text2.getText().trim().getBytes();            String str = new String(buf,0,buf.length);            System.out.println(str);            text1.append(str);            try{                OutputStream out = socket.getOutputStream();                                out.write((str+"\r\n").getBytes());                out.flush();            }catch(Exception e){}                    }    }//接收    class clientrec implements Runnable    {        public void run(){        byte[] buf = new byte[1024];        try{            InputStream in = socket.getInputStream();            in.read(buf);            String str = new String(buf,0,buf.length);            text1.append(str);        }catch(Exception e){}                }    }
------解决方案--------------------------------------------------------
用MAP做的:
Java code
public static void count(String info) {        Map<Character, Integer> map = new HashMap<Character, Integer>();        for (int i = 0; i < info.length(); i++) {            Integer count = map.get(info.charAt(i));            if (count == null) {                map.put(info.charAt(i), 1);            } else {                map.put(info.charAt(i), count + 1);            }        }        for (Character c : map.keySet()) {            System.out.println(c + " : " + map.get(c));        }    }
------解决方案--------------------------------------------------------
你代码没问题,除了那个cope,还有这样效率太低了。
  相关解决方案