0

私は自分の間違いを犯した場所を理解しようとしています。私はJavaが初めてで、入門クラスを行っています。基本的に、以前に作成したクラスを呼び出す countletterinanarray プログラムを作成する必要があります。ライブラリを無駄に追加しようとしましたが、それを機能させる方法がわかりません。私が受け取ったプログラム全体の唯一のエラーは、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');
    }
}
4

3 に答える 3

0

入力する必要があります

package randomcharacter;

あなたの呼び出しクラスで -CountLettersInArray

その後のみRandomCharacter.getRandomLowerCaseLetter()アクセス可能になります。

私はあなたのプログラムをチェックしました -

あなたの2つのクラスはそれぞれ -

1>RandomCharacter.java

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');
    }
}

2> CountLettersInArray.java

package randomcharacter;

/**
*
* @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') + " ");
           }
       }
}

次のような出力が表示されます-

The lowercase letters are:
z n s q a m a h w f f o c e f q i m e g
i g w k p q x f g z l h r c d g a a y l
q z n b y k i w n b b s z a k e z o e x
g b p b l t d o c n l q m h p m c t y q
b y y r k x c t i m f o q i g o k w c w

The occurences of each letter are:
5 a 6 b 6 c 2 d 4 e 5 f 6 g 3 h 5 i 0 j5 k 4 l 5 m 4 n 5 o 3 p 7 q 2 r 2 s 3 t0 u 0 v 5 w 3 x 5 y 5 z 

これで問題が解決することを願っています。

于 2013-03-10T15:49:17.867 に答える
0

最初のクラスはデフォルトのパッケージに含まれています。2 番目のクラスは package にありますrandomcharacter。2 番目のクラスは、最初のクラスのディレクトリ./randomcharacterである にある必要があります。.

Projectこれは、ビューとビューの両方で NetBeans で視覚的に確認できるはずですFiles

于 2013-03-10T15:51:58.550 に答える