各位大虾们,大家好,小弟我刚来论坛,还是菜鸟,望指点下面的程序,到底是什么地方出错,是什么问题(红色的地方无法通过调试),先谢了
import java.lang.Math;
public class Wang3
{
public static void main(String[] args)
{ RandomCharacter myCharacter = new RandomCharacter();
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] = myCharacter.getRandomCharacter();
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') + " ");
}
}
}
class RandomCharacter
{ char getRandomCharacter()
{ char ch1 = 'a';
char ch2 = 'z';
return (char)(ch1 + Math.random()*(ch2 - ch1 + 1));
}
}