1

如果用户输入一个字符串: hello there

它应该输出

Hello has 2 vowels
There has 3 consonants.

我知道这是一个相当简单的代码,但我得到了太多想法并且感到困惑。我需要一个来确保我有 2 个方法用于 numberofVowels 和 capitalizeWord 并且都返回一个结果

我遇到了一个错误,在我开始计算元音之后,我仍然想弄清楚是否要大写

import java.util.Scanner;

public class Hwk9
{
        public static void main (String args[]) 
        {
                Scanner stdin = new Scanner(System.in);
                String string1;
                System.out.println("Enter a string");
                string1 = stdin.nextLine();
                string1 = string1.toLowerCase();

        }
        public static int numberVowels(String string1)
        {

                int count = 0;
                int vowels = 0;
                int consonants = 0;
                for (int i = 0; i < string1.length(); i++)
                {
                        char ch = string1.charAt(i);
                        if (ch == 'a' || ch == 'e' || ch == 'i' || 
                                        ch == 'o' || ch == 'u')
                        {
                                vowels++;
                        }
                        else
                        { 
                                consonants++;
                        }
                }
        }
}
4

9 回答 9

2

这是一种更简单的方法,希望对您有所帮助:

public static void checkVowels(String s){
    System.out.println("Vowel Count: " + (s.length() - s.toLowerCase().replaceAll("a|e|i|o|u|", "").length()));
    //Also eliminating spaces, if any for the consonant count
    System.out.println("Consonant Count: " + (s.toLowerCase().replaceAll("a|e|i|o| |u", "").length()));
}
于 2015-03-02T18:21:47.387 回答
1

做了这样的事情希望这会有所帮助,这将给出元音,每个单词的辅音

public static void main (String args[]) 
        {
                Scanner stdin = new Scanner(System.in);
                String string1;
                System.out.println("Enter a string");
                string1 = stdin.nextLine();
                string1 = string1.toLowerCase();
                int count = 0;
                int vowels = 0;
                int consonants = 0;
                for (String retval: string1.split(" ")){
                     for (int i = 0; i < retval.length(); i++)
                {
                        char ch = retval.charAt(i);
                        if (ch == 'a' || ch == 'e' || ch == 'i' || 
                                        ch == 'o' || ch == 'u')
                        {
                                vowels++;
                        }
                        else
                        { 
                                consonants++;
                        }
                }
            System.out.println(retval.substring(0, 1).toUpperCase() + retval.substring(1)+" has "+vowels+" vowels and "+consonants+" cosonants");
         vowels=0;
         consonants=0;
      }

        }
于 2013-10-29T14:50:38.163 回答
1

我的代码没有扫描仪,可能不是很简单,但是:

public class Main{
public static void main(String[] args) {

    String test = "qwertyYVTA";
    test = test.trim();
    int vowels = 0;
    int consonants = 0;

    Pattern patternVow = Pattern.compile("[eyuioa]", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
    Matcher matcherVow = patternVow.matcher(test);
    while (matcherVow.find()) {
        vowels++;
    }

    Pattern patternCons = Pattern.compile("[wrtpsdfghjklzxcvbnm]", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
    Matcher matcherCons = patternCons.matcher(test);
    while (matcherCons.find()) {
        consonants++;
    }
    System.out.println("Vowels in test String is " + vowels);
    System.out.println("Consonants in test String is " + consonants);
    }
}
于 2015-11-21T16:51:44.527 回答
0

这是使用递归计算元音数量的简单代码

 public static int vowels(String s){
        int count =0;
        char c;
        if(s.length()==0){
            return 0;
        }
        else{
            c =s.charAt(0);
            if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'){
                count++;
            }
            return count+vowels(s.substring(1));


        }
    }
于 2015-03-27T20:34:30.593 回答
0

我建议你;

  • 创建一个最终元音数组,
  • 定义一个bool isVowel(char c)函数并在你的 if 条件中使用它。
于 2013-10-29T14:43:39.580 回答
0

以下代码将为您提供元音和辅音计数

static String VOWEL_GROUP = "AEIOUaeiou";
static String testString = "AAAASHMAIOUAXCCDIOUGGGGA"; // say this is your text

public static void main(String[] args) {
    int vovelCount = 0;
     int consonantCount = 0;
    for (int j = testString.length() - 1; j >= 0; j--) {//outer loop 
        for (int i = 0; i < VOWEL_GROUP.length(); i++) { //inner loop
            if (VOWEL_GROUP.charAt(i) == testString.charAt(j)) {
                vovelCount++; //vowel count in text
                break;
            }else{
                consonantCount ++;
            }
        }
    }
    System.out.println(vovelCount+" "+ consonantCount);
}
于 2013-10-29T14:47:28.477 回答
0

这看起来比上面的答案简单得多。它获取输入,将其转换为小写,然后转换为字符数组。一个简单的 for 循环就可以解决问题。

import java.util.*;

public class FindVowelsConsonents {

  public static void main(String[] args) {
    int vowels_count = 0;
    int consonents_count = 0;
    Scanner sc = new Scanner(System.in);
    String str = sc.nextLine();
    String str2 = str.toLowerCase();
    char[] chr = str2.toCharArray();

    for(int i=0;i<chr.length;i++){
        if(chr[i] == 'a' || chr[i]== 'e' || chr[i] == 'i' || 
                              chr[i] == 'o' || chr[i] == 'u')
            vowels_count++;
        else
            consonents_count++;
    }

    System.out.println(vowels_count+ " "+consonents_count); 
    sc.close();
   }
}
于 2017-03-29T23:07:14.633 回答
0

就我而言,您可以使用 StringTokenizer:

    String text = "dupalo twoja mama";
    StringTokenizer tokenizer = new StringTokenizer(text,"aeiuo",false);
    int vowels = tokenizer.countTokens();
    System.out.println(vowels);

在这种“文本”的情况下,它将打印出 7。

于 2017-09-11T20:33:19.590 回答
-1
import java.util.Scanner;

//don't use space in between the input string;

 class StrRev {

    static String Vowels ="aeiouAEIOU";

    public static void main(String[] args){
         @SuppressWarnings("resource")
        Scanner name = new Scanner(System.in);
         String nm = name.nextLine();

         System.out.println();
         int vowel=0;
         int consonant=0;
        for(int i=0;i<nm.length();i++){
            for(int j=0;j<Vowels.length();j++){
                if(Vowels.charAt(j)==nm.charAt(i)){
                    vowel++;
                    break;
                }
            }
        }
        consonant = nm.length()-vowel;
        System.out.println("no of Vowels :"+vowel+"\nno of consonant :"+consonant);
     }

}
于 2015-10-30T06:58:03.507 回答