返回0的元音和辅音计数器类

时间:2014-02-26 02:59:24

标签: java

我似乎在这里做错了什么。为了简短起见,我已经粘贴了类(VowCon),我相信我的错误,并在我的代码中进行了评论,以解释每种方法的作用。我怀疑我的问题特别是在数组和incrementor方法之间。当我从方法main输入一个字符串,并选择字符串中元音数量的选项时,它显示零元音和辅音。

public class VowCon
{
  private char[] array;
  int vowel = 0;
  int cons = 0;

  //constructor that accepts string and converts it to a char array
  public VowCon(String sentence)
  {
    array = sentence.toCharArray();
  }

  //method that increments the vowel and cons variables
  public void incrementor()
  {
  for (int i = 0; i < array.length; i++)
    if (isVowel(array[i]))
      vowel++;
    else if (isCons(array[i]))
      cons++;
  }

  //determines if each char in the array is a vowel
   private boolean isVowel(char c)
   {
        return (c == 'a' || c == 'e'
                || c == 'i' || c == 'o'
                || c == 'u');
    }

   //determines if each char in the array is a consonant
    private boolean isCons(char c)
    {
        return (((c >= 'a' && c <= 'z') || (c >= 'A' && c >= 'Z')) && !isVowel(c));
    }

    //returns value of vowel
    public int getVowels()
    {
      return vowel;
    }

    //returns value of cons
    public int getCons()
    {
      return cons;
    }
}

主程序

import java.util.Scanner;
public class ClassTest
{

   /**
    The printMenu methods displays a menu to the user
     */
    public static void printMenu() {
        System.out.println("Please select an option: ");
        System.out.println();
        System.out.print("a. Count the number of vowels in the string.\n"
                + "b. Count the number of consonants in the string.\n"
                + "c. Count both the vowels and consonants in the string.\n"
                + "d. Enter another string.\n"
                + "e. Exit the program\n");
    }
    public static void main(String[] args) {
        String input;      // to hold the user's input
        String option;     // to hold the user's input
        char choice;       // to hold a single character
        // create a Scanner object to read keyboard input.
        //  Scanner keyboard = new Scanner(System.in);
        Scanner keyboard;
 do {
            keyboard = new Scanner(System.in);
            // ask user to enter string
            System.out.print("Enter a string: ");
            input = keyboard.nextLine();
            input = input.toLowerCase();
            System.out.println();
            printMenu();
            option = keyboard.nextLine();
            choice = option.charAt(0);
            VowCon words = new VowCon(input);
            switch (choice) {
                case 'a':
                case 'A':
                    System.out.println("Number of Vowels: " + words.getVowels());
                    break;
                case 'b':
                case 'B':
                    System.out.println("Number of Consonants: " + words.getCons());
                    break;
                case 'c':
                case 'C':
                    System.out.println("Number of Vowels & Consonants: \n " + words.getCons() + " Consonants\n " +
                                      words.getVowels() + " Vowels");
                    break;
                case 'd':
                case 'D':
                    System.out.println("Enter a string: ");
                    break;
                case 'e':
                case 'E':
                    System.exit(0);
                    break;
                default:
                    System.out.println("You did not enter a valid choice.");
            }
            //
            // keyboard.nextLine();    //consumes the new line character after the choice
            // String answer = keyboard.nextLine();
            // letter = answer.charAt(0);
        } while (true);
    }
}

3 个答案:

答案 0 :(得分:2)

我的猜测是,在请求元音或辅音计数之前,不要调用incrementor()方法。

暂且不谈 -

  • 你的isVowel仅适用于小写元音。你最好概括一下。
  • 你真的应该告诉我们你如何测试代码以获得更好的答案。

答案 1 :(得分:2)

罪魁祸首是:

   private boolean isCons(char c)
{
    return (((c >= 'a' && c <= 'z') || (c >= 'A' && c >= 'Z')) && !isVowel(c));
}

条件:c&gt; ='A'&amp;&amp; c&gt; ='Z'应为c&gt; ='A'&amp;&amp; c&lt; ='Z'

同时更改isVowel()函数以使其适用于大写字母。

答案 2 :(得分:0)

你最好踩过“句子”,如下所示

public class CountVowelConsonent {
private static final String vowels = "aeiouAEIOU";
private static final String consonents = "bcdfghijklmnpqrstvwxyzBCDFGHIJKLMNPQRSTVWXYZ";
private int consonentCount = 0;
private int vowelCount = 0;
private String sentence;

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    CountVowelConsonent me = new CountVowelConsonent();
    String trial = "Now is the time for all good men to come to the aid of their country.";
    me.setSentence(trial);
    System.out.println("Vowels = " + me.getVowelCount() + ", Consonents = "
            + me.getConsonentCount());
    System.out.println(" " + trial.length());

}

public CountVowelConsonent() {
}

public void setSentence(String sentence) {

    this.sentence = sentence.trim();

    for (int i = 0; i < sentence.length(); i++) {
        String letter = this.sentence.substring(i, i + 1);
        if (vowels.indexOf(letter) > -1) {
            vowelCount++;
        } else if (consonents.indexOf(letter) > -1) {
            consonentCount++;
        }
    }

}

public int getVowelCount() {
    return vowelCount;
}

public int getConsonentCount() {
    return consonentCount;
}

}