我一直在试图找出我在哪里犯了错误。我是 Java 新手,正在上入门课。基本上我必须创建一个 countletteranarray 程序,在其中调用我之前创建的类。我试图添加库无济于事,无法弄清楚如何让它工作。我收到的整个程序中唯一的错误是无法为 randomcharacter.RandomCharacter 添加导入。下面是我的代码:package countlettersinarray;
/**
*
* @author james
*/
public class CountLettersInArray {
public static void main(String[] args) {
//Declare and create an array
char[] chars = createArray();
//Display the array
System.out.println("The lowercase letters are:");
displayArray(chars);
//Count the occurences of each letter
int[] counts = countLetters(chars);
//Displahy counts
System.out.println();
System.out.println("The occurences of each letter are:");
displayCounts(counts);
}
/**Create an array of characters**/
public static char[] createArray() {
//Declare an array of characters and create it
char[] chars = new char[100];
//Create lower case letters randomly and assign
//them to the array
for (int i = 0; i < chars.length; i++)
chars[i] = RandomCharacter.getRandomLowerCaseLetter();
//Return the array
return chars;
}
public static void displayArray(char[] chars) {
//Display characters in the array 20 on each line
for (int i = 0; i < chars.length; i++) {
if ((i + 1) % 20 == 0)
System.out.println(chars[i]);
else
System.out.print(chars[i] + " ");
}
}
/** Count the occurences of each letter*/
public static int[] countLetters(char[] chars) {
//Declare and create an array of 26 int
int[] counts = new int[26];
//For each lower case letter in the array, count it
for (int i = 0; i < chars.length; i++)
counts[chars[i] - 'a']++;
return counts;package countlettersinarray;
/**
*
* @author james
*/
public class CountLettersInArray {
public static void main(String[] args) {
//Declare and create an array
char[] chars = createArray();
//Display the array
System.out.println("The lowercase letters are:");
displayArray(chars);
//Count the occurences of each letter
int[] counts = countLetters(chars);
//Displahy counts
System.out.println();
System.out.println("The occurences of each letter are:");
displayCounts(counts);
}
/**Create an array of characters**/
public static char[] createArray() {
//Declare an array of characters and create it
char[] chars = new char[100];
//Create lower case letters randomly and assign
//them to the array
for (int i = 0; i < chars.length; i++)
chars[i] = RandomCharacter.getRandomLowerCaseLetter();
//Return the array
return chars;
}
public static void displayArray(char[] chars) {
//Display characters in the array 20 on each line
for (int i = 0; i < chars.length; i++) {
if ((i + 1) % 20 == 0)
System.out.println(chars[i]);
else
System.out.print(chars[i] + " ");
}
}
/** Count the occurences of each letter*/
public static int[] countLetters(char[] chars) {
//Declare and create an array of 26 int
int[] counts = new int[26];
//For each lower case letter in the array, count it
for (int i = 0; i < chars.length; i++)
counts[chars[i] - 'a']++;
return counts;
}
/**Display counts*/
public static void displayCounts(int[] counts) {
for (int i = 0; i < counts.length; i++) {
if ((i + 1) % 10 == 0)
System.out.print((counts[i] + " " + (char)(i + 'a')));
else
System.out.print(counts[i] + " " + (char)(i + 'a') + " ");
}
}
}
}
/**Display counts*/
public static void displayCounts(int[] counts) {
for (int i = 0; i < counts.length; i++) {
if ((i + 1) % 10 == 0)
System.out.print((counts[i] + " " + (char)(i + 'a')));
else
System.out.print(counts[i] + " " + (char)(i + 'a') + " ");
}
}
}
这是 RandomCharacter.getRandomLowerCaseLetter(); 我想打电话的班级:
package randomcharacter;
/**
*
* @author james
*/
public class RandomCharacter {
/**Generate a random character between ch1 and ch2**/
public static char getRandomCharacter(char ch1, char ch2) {
return (char)(ch1 + Math.random() * (ch2 - ch1 + 1));
}
//Generate a lower case letter
public static char getRandomLowerCaseLetter() {
return getRandomCharacter('a', 'z');
}
//Generate an upper case letter
public static char getRandomUpperCaseLetter() {
return getRandomCharacter('A', 'B');
}
//Generate a random number
public static char getRandomDigitCharacter() {
return getRandomCharacter('0', '9');
}
//Generate a random character
public static char getRandomCharacter() {
return getRandomCharacter('\u0000', '\uFFFF');
}
}