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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ash 中级黑马   /  2015-11-14 09:52  /  426 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

public class CountLettersInArray
{
public static void main(String[] args)
{
  char [] chars = createArray() ;
  System.out.println("The lowercase letters are:");
  displayArray(chars) ;

  int [] counts = countLetters(chars) ;

  System.out.println() ;
  System.out.println("The occurrences of each letter are:") ;
  displayCounts(counts);
}

public static char[] createArray()
{
  char[] chars = new char[100] ;
  for (int i = 0;i<chars.length ;i++ )
   chars[i] = RandomCharacter.getRandomLowerCaseLetter() ;

  return chars;
}

public static void displayArray(char[] chars)
{
  for (int i = 0;i<chars.length ;i++ )
  {
   if ((i + 1)%20==0)
    System.out.println(chars[i] + "  ") ;
   else
    System.out.print(chars[i] + "  ") ;
  }
}

public static int[] countLetters(char[] chars)
{
  int[] counts = new int[26] ;

  for (int i = 0 ;i<chars.length ;i++ )
   counts[chars[i] - 'a']++ ;

  return counts ;
}

public static void displayCounts(int[] counts)
{
  for (int i=0;i<counts.length ;i++ )
  {
   if ((i + 1)%10 == 0)
    System.out.println(counts[i] + "  "
   + (char)(i + 'a')) ;
   else
    System.out.print(counts[i] + "  "
   + (char)(i + 'a') + "  ") ;
  }
}
}

程序运行不出来,帮帮忙

2 个回复

倒序浏览
这样没层次感地去看,真的很累,望楼主改进
回复 使用道具 举报
二楼很对,下次贴代码可以在编辑区的代码区域添加,可读性更好(如果你是真心想让别人去为你解答的话)我对你原代码改了一处,有注释,你看下


  1. public class Test {
  2.         public static void main(String[] args) {
  3.                 char[] chars = createArray();
  4.                 System.out.println("The lowercase letters are:");
  5.                 displayArray(chars);

  6.                 int[] counts = countLetters(chars);

  7.                 System.out.println();
  8.                 System.out.println("The occurrences of each letter are:");
  9.                 displayCounts(counts);
  10.         }

  11.         public static char[] createArray() {
  12.                 char[] chars = new char[100];
  13.                 for (int i = 0; i < chars.length; i++)
  14.                         //chars[i] = RandomCharacter.getRandomLowerCaseLetter();     
  15.                         chars[i] ='i';//将你原程序的这一行改为我这一行,发现程序可正常跑起来,然后你自己看能不能发现问题,不能再问
  16.                 return chars;
  17.         }

  18.         public static void displayArray(char[] chars) {
  19.                 for (int i = 0; i < chars.length; i++) {
  20.                         if ((i + 1) % 20 == 0)
  21.                                 System.out.println(chars[i] + "  ");
  22.                         else
  23.                                 System.out.print(chars[i] + "  ");
  24.                 }
  25.         }

  26.         public static int[] countLetters(char[] chars) {
  27.                 int[] counts = new int[26];

  28.                 for (int i = 0; i < chars.length; i++)
  29.                         counts[chars[i] - 'a']++;

  30.                 return counts;
  31.         }

  32.         public static void displayCounts(int[] counts) {
  33.                 for (int i = 0; i < counts.length; i++) {
  34.                         if ((i + 1) % 10 == 0)
  35.                                 System.out.println(counts[i] + "  " + (char) (i + 'a'));
  36.                         else
  37.                                 System.out.print(counts[i] + "  " + (char) (i + 'a') + "  ");
  38.                 }
  39.         }
  40. }
复制代码




回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马