A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 肤浅 初级黑马   /  2013-12-21 21:45  /  1191 人查看  /  10 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

1、编写一个程序,用一个数组来记录一个字符串中每个字母出现的次数,并输出结果,该字符串从键盘接收。

评分

参与人数 1黑马币 +5 收起 理由
乔兵 + 5

查看全部评分

10 个回复

倒序浏览
  1. public class Demo4{
  2.     public static void main(String[] args){

  3.             stringCount();
  4.     }
  5.     public static void stringCount(){
  6.             String str="ewqr2345432ewtfaewfads";
  7.             char[] ch=str.toCharArray();
  8.             char[] chs=new char[str.length()];
  9.             int m=0;
  10.             w:for(int i=0;i<str.length();i++){
  11.                     for(int j=0;j<chs.length;j++)
  12.                     {
  13.                             if(chs[j]==ch[i])
  14.                                 continue w;
  15.                     }                                         
  16.                     int count=0;
  17.                     int index=0;                    
  18.                     while((index=str.indexOf(ch[i],index))!=-1){
  19.                             index++;
  20.                             count++;
  21.                     }
  22.                     chs[m++]=ch[i];
  23.                  
  24.                     System.out.println(ch[i]+":"+count);
  25.             }
  26.            
  27.    }
  28. }
复制代码

  1. import java.util.HashMap;
  2. import java.util.Iterator;
  3. import java.util.Set;


  4. public class Demo3 {

  5.         public static void main(String[] args) {
  6.                 HashMap<String,String> nums = new HashMap<String,String>();
  7.                 String str="ewqr2345432ewtfaewfads";
  8.                
  9.                
  10.                 for(int i =0;i<str.length();i++){
  11.                         char c = str.charAt(i);
  12.                         String s = String.valueOf(c);
  13.                         if(nums.containsKey(s)){
  14.                                 String n = nums.get(s);
  15.                                 int num = Integer.parseInt(n);
  16.                                 nums.put(s,String.valueOf(++num));
  17.                         }else{
  18.                                 nums.put(s, "1");
  19.                         }
  20.                 }
  21.                 Set<String> set = nums.keySet();
  22.                 Iterator<String> it = set.iterator();
  23.                 while(it.hasNext()){
  24.                         String key  = it.next();
  25.                         System.out.println(key+":"+nums.get(key));
  26.                 }
  27.                
  28.         }

  29. }
复制代码
回复 使用道具 举报
楼主代码如下,如果有不明白的可以再问我:
  1. package test;

  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.util.ArrayList;
  6. import java.util.List;


  7. /**
  8. * 编写一个程序,用一个数组来记录一个字符串中每个字母出现的次数,并输出结果,该字符串从键盘接收
  9. * @author Xing,Ming
  10. * @version 2013年12月22日 上午12:27:56
  11. *
  12. * 思路:
  13. * 1、创建一个键盘读取流,用于从键盘接收数据
  14. * 2、BufferedReader中的readLine方法返回值是String类型的,用一个变量进行接收
  15. * 3、编写一个字母出现次数的方法
  16. */
  17. public class CharTimes {

  18.         public static void main(String[] args) {
  19.                 // TODO Auto-generated method stub
  20.                 IO2String();
  21.                
  22.         }
  23.        
  24.         public static void IO2String() {
  25.                 //创建一个缓冲读取流
  26.                 BufferedReader br = null;
  27.                 //创建一个接收键盘输入数据的字符串变量
  28.                 String str = null;
  29.                 //创建一个接收最终要输出的结果的字符串类型的数组集合
  30.                 List<String> as = new ArrayList<String>();
  31.                 //创建从键盘接收数据的IO流对象
  32.                 try {
  33.                         System.out.println("请输入数据:");
  34.                         br = new BufferedReader(new InputStreamReader(System.in));
  35.                         //循环读取键盘录入数据
  36.                         while((str = br.readLine()) != null) {
  37.                                 //如果读取到的录入数据是“exit”,就结束方法
  38.                                 if(str.equalsIgnoreCase("exit")) {
  39.                                         System.out.println("程序结束!!!!!");
  40.                                         break;
  41.                                 }
  42.                                 as = getLetterCount(str);
  43.                                 //利用集合的toArray方法,将集合转换成数组
  44.                                 String[] s = as.toArray(new String[as.size()]);
  45.                                 System.out.println("字母出现次数如下:");
  46.                                 for (int i = 0; i < s.length; i++) {
  47.                                         System.out.print(s[i] + "  ");
  48.                                 }
  49. //                                System.out.println(as);
  50.                                 System.out.println();
  51.                                 System.out.println();
  52.                                 System.out.println("请输入数据:");
  53.                         }
  54.                 } catch (IOException e) {
  55.                         // TODO Auto-generated catch block
  56.                         e.printStackTrace();
  57.                         System.out.println(e.toString());
  58.                 }
  59.         }
  60.        
  61.         //编写字母出现次数的方法
  62.         public static List<String> getLetterCount(String str) {
  63.                 //创建一个记录字母出现的下标的变量
  64.                 int index = 0;
  65.                
  66.                 //将字符串数据转变成StringBuilder数据
  67.                 //方便使用StringBuilder的deleteCharAt方法删除特定的字母
  68.                 StringBuilder sb = new StringBuilder(str);
  69.                 //创建一个接收最终结果的字符串类型的数组集合
  70.                 List<String> as = new ArrayList<String>();
  71.                
  72.                 //循环判断小写字母是否出现,且出现的次数
  73.                 for (char i = 'a'; i < (char)('z'+1); i++) {
  74.                         //创建一个记录次数的变量
  75.                         int count = 0;
  76.                         //将字符转变成字符串
  77.                         String letter = i + "";
  78.                        
  79.                         //循环判断字符串中是否出现小写字母
  80.                         while((index = sb.indexOf(letter)) != -1) {
  81.                                 //若出现则次数加1
  82.                                 ++count;
  83.                                 //将字符串中查到的第一个特定小写字母删除,再去查找
  84.                                 sb = sb.deleteCharAt(index);
  85.                         }
  86.                        
  87.                         //将出现次数不为0的小写字母,加入集合中
  88.                         if(count != 0)
  89.                                 as.add(letter + count);
  90.                 }
  91.                
  92.                 //循环判断大写字母是否出现,且出现的次数
  93.                 for (char i = 'A'; i < (char)('Z'+1); i++) {
  94.                         int count = 0;
  95.                         String letter = i + "";
  96.                         while((index = sb.indexOf(letter)) != -1) {
  97.                                 ++count;
  98.                                 sb = sb.deleteCharAt(index);
  99.                         }
  100.                         if(count != 0)
  101.                                 as.add(letter + count);
  102.                 }
  103.                
  104.                 return as;
  105.         }

  106. }
复制代码
回复 使用道具 举报
我记得是这样:
import java.util.Scanner;
public class Test {
    public static void main(String[] args) {
        char c = 'a';
        int[] counts = new int['z' - 'a'];
        System.out.print("your input :");
        Scanner sc = new Scanner(System.in);
        String line = sc.nextLine();
        line = line.toLowerCase();
        line = line.replaceAll("[^a-z]", "");
        System.out.println(line);
        char[] array = line.toCharArray();
        for (int i = 0; i < array.length; i++) {
            counts[array[i] - c]++;
        }
        System.out.println("char count:");
        for (int i = 0; i < counts.length; i++) {
            System.out.println((char) (c + i) + " : " + counts[i]);
        }
    }
}
回复 使用道具 举报
aion2013tian 发表于 2013-12-22 13:51
楼主代码如下,如果有不明白的可以再问我:

恩恩,我试试,我是黑马ee已经毕业的学员。我只是不想写。如果你以后什么需要我帮忙的。可以直接给我说!
回复 使用道具 举报

恩恩,我试试,我是黑马ee已经毕业的学员。我只是不想写。如果你以后什么需要我帮忙的。可以直接给我说!
回复 使用道具 举报
肤浅 发表于 2013-12-22 22:10
恩恩,我试试,我是黑马ee已经毕业的学员。我只是不想写。如果你以后什么需要我帮忙的。可以直接给我说! ...

嗯嗯好的,我好久没学了,哈哈,主要是大四上学期有别的事要忙,我都快把基础忘了,我也想进黑马,看着这个培训机构不错 把,,我qq1149558001
回复 使用道具 举报
肤浅 发表于 2013-12-22 22:07
恩恩,我试试,我是黑马ee已经毕业的学员。我只是不想写。如果你以后什么需要我帮忙的。可以直接给我说! ...

好的,我也是很想去黑马的啊 现在也是在努力自学中,在长技术分的啊 希望早日去黑马的啊
回复 使用道具 举报
肤浅 初级黑马 2013-12-27 00:05:58
9#
aion2013tian 发表于 2013-12-23 09:46
好的,我也是很想去黑马的啊 现在也是在努力自学中,在长技术分的啊 希望早日去黑马的啊 ...

加油!@@@
回复 使用道具 举报
肤浅 初级黑马 2013-12-31 21:25:39
10#
aion2013tian 发表于 2013-12-22 13:51
楼主代码如下,如果有不明白的可以再问我:

来我群246891574
回复 使用道具 举报
肤浅 初级黑马 2013-12-31 21:29:34
11#
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马