如何计算元音和辅音,并在使用方法和返回结果输出时将字符串中的第一个字母大写

如果用户输入一个字符串: 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++; } } } } 

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

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

做了这样的希望,这有助于,这将给每个单词的元音,辅音

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

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

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

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

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

我建议你;

  • 创建一个最终的元音数组,
  • 定义一个bool isVowel(char c)函数并在if条件中使用它。

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

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

这看起来比上面的答案简单。 它获取输入,将其转换为小写,然后转换为字符数组。 一个简单的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 

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

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

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

 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