Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过netbeans导入类_Java_Class_Methods - Fatal编程技术网

Java 通过netbeans导入类

Java 通过netbeans导入类,java,class,methods,Java,Class,Methods,我一直在努力找出我犯错误的地方。我是java新手,正在上入门课。基本上我必须创建一个数组程序,在这里我调用一个我以前创建的类。我试图添加该库,但没有任何效果,也不知道如何让它工作。我收到的整个程序中唯一的错误是无法为randomcharacter.randomcharacter添加导入。下面是我的代码: 包计数数组 /** * * @author james */ public class CountLettersInArray { public static void main

我一直在努力找出我犯错误的地方。我是java新手,正在上入门课。基本上我必须创建一个数组程序,在这里我调用一个我以前创建的类。我试图添加该库,但没有任何效果,也不知道如何让它工作。我收到的整个程序中唯一的错误是无法为randomcharacter.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;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') + " ");
            }
        }
}
你应该进去

package randomcharacter;
在调用类中,即-
CountletsInArray

然后,只有
RandomCharacter.getRandomLowerCaseLetter()
才可访问

我查过你的程序了-

你们两个班将分别上课-

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

希望这能解决您的问题。

您的第一堂课在默认包中。您的第二个类在package
randomcharacter
中。您的第二个类应该位于目录
/randomcharacter
中,其中
是您的第一个类的目录


您应该能够在NetBeans中的
项目
文件
视图下直观地看到这一点。

而不是使用RandomCharacter.getRandomLowerCaseLetter()

只需使用随机类

char[] chars = new char[100];

        for (int i = 0; i < chars.length; i++) {

            Random random = new Random();

            char randomChar = (char) (random.nextInt(26) + 'a');

            chars[i] = randomChar;
        }
char[]chars=新字符[100];
for(int i=0;i

此代码将解决您的问题,并提供与您所需相同的输出。

您只需将另一个类放入同一个项目中的
src/randomCharacter
。我已尝试返回,重新编译随机字符程序,在“项目”选项卡中选择CountLetterInArray项目,选择库,右键单击,添加项目,选择随机字符项目。它仍然没有导入类。我只是注意到randomcharacter程序中没有主方法,这可能是我的问题吗?无论出于什么原因,我仍然无法让这个程序工作。我的书让我创建随机字符文件,保存它,然后创建CountLettersInArray文件。当我尝试运行CountLettersInArray文件时,唯一的错误是RandomCharacter下的红线。我应该把这两个文件合并成一个文件来工作吗?不,把它们分开——RandomCharacter.java和CountleteSinArray.java,然后把它们放在一个包中——RandomCharacter明白了,我想我一定错过了NetBeans教程中的那部分。谢谢大家的帮助。
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 
char[] chars = new char[100];

        for (int i = 0; i < chars.length; i++) {

            Random random = new Random();

            char randomChar = (char) (random.nextInt(26) + 'a');

            chars[i] = randomChar;
        }