【问题标题】:How to create a method to count characters in an array如何创建一种方法来计算数组中的字符
【发布时间】:2015-11-13 02:14:47
【问题描述】:

所以我有一个方法可以创建一个由 100 个随机生成的字符组成的数组。

这里是:

//method to generate random character between ch1 and ch2
   public static char getRandomCharacter(char ch1, char ch2)
   {

      return (char) (ch1 + Math.random() * (ch2 -ch1 +1));

   }

//==========================================   

   //method to assign generated characters (between a and z) to a 100 character array
   public static char[] createArray()
   {

      //declare a 100 character array
      char[] character = new char[100];

      //for loop assigning the random characters to the array using getRandomCharacter method
      for (int x = 0; x < character.length; x++)
      character[x] = getRandomCharacter('a', 'z');

      //for loop outputting the characters in the array
      for (int x = 0; x < character.length; x++)
      System.out.println(character[x]);

      return character;

   }

现在我需要创建一个方法来获取 100 个生成的字符,并计算每个元音生成的次数。我被这个困住了。

这是我目前所拥有的:

public static void countArray()
   {

      int vowelA, vowelE, vowelI, vowelO, vowelU, vowelY;
      int elsePlaceHolder = 0;

      for (int x = 0; x < 100; x++)
      {

         if ((createArray()) == ('a'))
         vowelA++;

         else if ((createArray()) == ('e'))
         vowelE++;

         else if ((createArray()) == ('i'))
         vowelI++;

         else if ((createArray()) == ('o'))
         vowelO++;

         else if ((createArray()) == ('u'))
         vowelU++;

         else if ((createArray()) == ('y'))
         vowelY++;

         else
         elsePlaceHolder++;

      }

我相信我使用 for 循环来执行此操作是正确的,但我认为我没有正确执行它。一旦执行,我可以显示使用 int 变量计算元音的次数。 elsePlaceHolder 变量在那里,因为我不知道如何处理 else 语句。

谁能用我的countArray() 方法指出我正确的方向?将不胜感激!

【问题讨论】:

    标签: java arrays loops methods


    【解决方案1】:

    我觉得你应该把上面的代码修改成这个!

    public static void countArray()
    {
    
      int vowelA=0, vowelE=0, vowelI=0, vowelO=0, vowelU=0, vowelY=0;
      int elsePlaceHolder = 0;
      char [] arr = new char[100];
      arr=createArray();
      for (int x = 0; x < 100; x++)
      {
    
         if (arr[x] == 'a')
         vowelA++;
    
         else if (arr[x] == 'e')
         vowelE++;
    
         else if (arr[x] == 'i')
         vowelI++;
    
         else if (arr[x] == 'o')
         vowelO++;
    
         else if (arr[x] == 'u')
         vowelU++;
    
         else if (arr[x] == 'y')
         vowelY++;
    
         else
         elsePlaceHolder++;
    
      }
      System.out.print(vowelA+" "+vowelE+" "+vowelI+" "+vowelO+" "+vowelU+" "+vowelY);
    }
    

    它会起作用的。另外,您还没有初始化那些元音迭代器,但是您已经增加了它们!

    【讨论】:

    • 谢谢你,唯一的问题是我必须有一个单独的方法 (createArray()) 来创建 100 个字符的数组并输出 100 个字符。
    • 我是否可以保留我的 createArray() 和 countArray() 方法?
    • 抱歉兄弟没明白你说的 keep 是什么意思?对于主函数,您只需要: public static void main(String []as){ countArray();在 countArray() 中你已经调用了 createArray() 。
    • 抱歉,我可能没有正确理解您的回答。您让我在 countArray() 方法中创建 100 个字符的数组,但我必须在 createArray() 方法中创建该数组。还是我读错了,我只需要将我的 countArray() 方法更改为与上面的方法相同?
    • 没关系,我看到当您创建 100 个字符的数组时,您调用 createArray() 方法来填充它。感谢您的回复,您解决了我的问题。
    【解决方案2】:

    您将创建一次数组并计算元素的数量。

    char[] chars = createArray();
    
    for(int i = 0; i < chars.length; i++){
        if(chars[i] == 'a')
            increment number of A's
        else if ...
    }
    

    我会创建一个大小为 5 的数组,而不是创建 5 个元素,a 是 arr[0],e 是 arr[1] 等等。

    在您的代码中

    int[] arr = new int[5];
    for(int i = 0; i < chars.length; i++){
        if(chars[i] == 'a')
            arr[0]++;
        else if(chars[i] == 'e')
            arr[1]++;
        //etc
    }
    

    【讨论】:

    • 我假设您的意思是我的方法 createArray(),而不是 generateChar()?它确实有效。所以,如果我明白你在说什么,你是说我不需要我的 createArray() 方法?我的指令是创建一个方法 (createArray()),它创建一个 100 个字符的数组,然后输出这 100 个字符。然后我必须创建一个方法 (countArray()) 来计算每个元音在数组中生成的次数。所以我不能省略那个方法..
    【解决方案3】:
        char[] randomc = new char[]{'a','b','i','i','o','u','u','e','e','e','e','e','e','z','b','f'};
        int[] voweltable = new int[] {0, 0, 0, 0, 0, 0}; // Last element is for non-vowels
        for (int i = 0; i <= randomc.length - 1; i++)
        {
            switch(randomc[i])
            {
                case 'a':
                    voweltable[0]++;
                    break;
                case 'e':
                    voweltable[1]++;
                    break;
                case 'i':
                    voweltable[2]++;
                    break;
                case 'o':
                    voweltable[3]++;
                    break;
                case 'u':
                    voweltable[4]++;
                    break;
                default:
                    voweltable[5]++;
                    break;
            }
        }
        for (int i = 0; i <= voweltable.length -1; i++)
            System.out.println(voweltable[i]);
    

    【讨论】:

      【解决方案4】:

      getRandomCharacter()方法相同,你可以尝试利用字符的int值,并为它构建一个int数组,其大小在ch1ch2之间,然后增加每次出现的值,然后打印元音的计数,例如:

      public static void countArray(char ch1, char ch2, char[] character)
          {
              // Create an array with the letters you want to compare with: vowels
              int[] vowels = {(int) 'a', (int) 'e', (int) 'i', (int) 'o', (int) 'u'};
      
              // Create the array for the range between the two letters
              int[] dictionary = new int[(int) ch2 - (int) ch1 + 1];
      
              // Fill up the array with the ocurrence of each character
              for(int i = 0; i < character.length; i++){
                  dictionary[((int)character[i] - (int) ch1)]++;
              }
      
              // Print the occurrence of each vowel
              for(int j = 0; j < vowels.length; j++){
                  System.out.println((char) vowels[j] + ": " + dictionary[vowels[j] - (int)'a']);
              }
          }
      

      【讨论】:

        【解决方案5】:

        您好,我只是稍微更改了您的代码。 希望对你有帮助。 看看这个!

        private static int vowelA=0;
        private static int vowelE=0;
        private static int vowelI=0; 
        private static int vowelO=0;
        private static int vowelU=0;
        private static int vowelY=0;
        private static int elsePlaceHolder = 0;
        
        public static void main(String[] args) {
        
                char[] chs = createArray();
                System.out.println("vowelA : "+vowelA);
                System.out.println("vowelE : "+vowelE);
                System.out.println("vowelI : "+vowelI);
                System.out.println("vowelO : "+vowelO);
                System.out.println("vowelU : "+vowelU);
                System.out.println("vowelY : "+vowelY);
        }
        
        //method to generate random character between ch1 and ch2
           public static char getRandomCharacter(char ch1, char ch2)
           {
        
              return (char) (ch1 + Math.random() * (ch2 -ch1 +1));
        
           }
        
        //==========================================   
        
           //method to assign generated characters (between a and z) to a 100 character array
           public static char[] createArray()
           {
        
              //declare a 100 character array
              char[] character = new char[100];
        
              //for loop assigning the random characters to the array using getRandomCharacter method
              for (int x = 0; x < character.length; x++){
              character[x] = getRandomCharacter('a', 'z');
              countArray(character[x]);
              }
              //for loop outputting the characters in the array
              for (int x = 0; x < character.length; x++)
              System.out.println(character[x]);
        
              return character;
        
           }
        
           public static void countArray(char ch)
           {
        
                 if (ch == ('a')){
                 vowelA++;}
                 else if (ch == 'e'){
                 vowelE++;}
                 else if (ch == 'i'){
                 vowelI++;}
                 else if (ch == 'o'){
                 vowelO++;}
                 else if (ch == 'u'){
                 vowelU++;}
                 else if (ch == 'y'){
                 vowelY++;}
                 else{
                 elsePlaceHolder++;}
           }
        

        【讨论】:

        • 您应该解释您作为答案发布的代码。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-15
        相关资源
        最近更新 更多