Java计算元音程序

Java计算元音程序,java,Java,我正在尝试制作一个程序,让用户输入一个单词,然后它将计算该单词中元音和辅音的数量,并打印数量。e、 g:YOURWORD这个词包含3个元音和5个辅音 因为辅音就是不是元音的每个字母,我只让程序检查单词中是否有元音,然后辅音的数量就是单词中其他字母的数量。然而,我正在与for循环作斗争。这是我的密码: String word; Scanner myinput = new Scanner(System.in); System.out.println("Please enter

我正在尝试制作一个程序,让用户输入一个单词,然后它将计算该单词中元音和辅音的数量,并打印数量。e、 g:YOURWORD这个词包含3个元音和5个辅音

因为辅音就是不是元音的每个字母,我只让程序检查单词中是否有元音,然后辅音的数量就是单词中其他字母的数量。然而,我正在与for循环作斗争。这是我的密码:

   String word;
    Scanner myinput = new Scanner(System.in);

    System.out.println("Please enter a word.");
    word = myinput.next();
    char[] wordc = word.toCharArray();

    for(int w = 0;w > word.length();w++;) {
      if(wordc[w] == 'a' || wordc[w] == 'e' || wordc[w] == 'i' || wordc[w] == 'o' || wordc[w] == 'u') {

      }

正如你所看到的,我已经接近尾声了,但我现在真的不知道该做什么。我是Java初学者,我已经检查了for循环语法,但我真的不知道该怎么做,请帮助。

您应该定义两个int变量来进行计数。 for循环如下所示

   String word;
int numberOfVowls = 0; numberofCosonents = 0;
    Scanner myinput = new Scanner(System.in);

    System.out.println("Please enter a word.");
    word = myinput.next();
    char[] wordc = word.toCharArray();

    for(int w = 0;w < word.length();w++;) {
      if(wordc[w] == 'a' || wordc[w] == 'e' || wordc[w] == 'i' || wordc[w] == 'o' || wordc[w] == 'u') numberOfVowls++;
else numberofCosonents++;}
字符串字;
int numberOfVowls=0;余元数=0;
扫描仪myinput=新扫描仪(System.in);
System.out.println(“请输入一个单词”);
word=myinput.next();
char[]wordc=word.toCharArray();
for(int w=0;w
尝试更改

for(int w = 0;w > word.length();w++)


这里有一种不用循环和多种条件来计算元音和辅音的方法

String word = "YOURWORD"; // assuming only letters

String vowels = word.replaceAll( "(?i)[^aeiou]+", "" ); // OUO

int numVowels     = vowels.length(); // 3
int numConsonants = word.length() - numVowels; // 5
工作原理:

word.replaceAll(“(?i)[^aeiou]+”,”)
调用中,我们正在替换不是
[^aeiou]
之一的所有内容,从而在返回的字符串中只留下元音。

string[]arr=text.split(”;
String[] arr = text.split(" ");
     char elementch = ' ';

     //looping through string array
    for(int i=1; i<= arr.length ; i++){
        String nextElement = arr[i-1];

        int add = 0;
            //looping through each character in the next element
            for (char ch: nextElement.toCharArray()) {
                //checking if ch == to vowels
                if(ch == 'e'|| ch == 'a' || ch == 'o' || ch == 'u'|| ch == 'i'){
                    //add counts number of vowels for every string array index
                    add = add +1;
                    elementch = ch; 
                    System.out.print(" ' " +elementch + " ' ");
                }else{
                    //do nothing
                }
            }
            System.out.println("The number of vowels is "+ add + " in" + " : " + nextElement.toUpperCase());
    }
char elementch=''; //通过字符串数组循环
对于(int i=1;它是一个无限循环。应该是
w
^来澄清;从技术上讲,它不是一个无限循环,它是一个条件循环,永远不会满足,因此永远不会执行。但是,如果w被实例化为大于word.length()的值,并且需要更改,它确实有可能成为一个无限循环。
String word;
Scanner myinput = new Scanner(System.in);

System.out.println("Please enter a word.");
word = myinput.next();
char[] wordc = word.toCharArray();

int vowels = 0;

for (char w: wordc) {
    if(w == 'a' || w == 'e' || w == 'i' || w == 'o' || w == 'u') {
        vowels++;
    }
}

System.out.println("Number of vowels in " + word + " is: " + vowels);
System.out.println("Number of consonants in " + word + " is: " + (wordc.length - vowels));
String word = "YOURWORD"; // assuming only letters

String vowels = word.replaceAll( "(?i)[^aeiou]+", "" ); // OUO

int numVowels     = vowels.length(); // 3
int numConsonants = word.length() - numVowels; // 5
String[] arr = text.split(" ");
     char elementch = ' ';

     //looping through string array
    for(int i=1; i<= arr.length ; i++){
        String nextElement = arr[i-1];

        int add = 0;
            //looping through each character in the next element
            for (char ch: nextElement.toCharArray()) {
                //checking if ch == to vowels
                if(ch == 'e'|| ch == 'a' || ch == 'o' || ch == 'u'|| ch == 'i'){
                    //add counts number of vowels for every string array index
                    add = add +1;
                    elementch = ch; 
                    System.out.print(" ' " +elementch + " ' ");
                }else{
                    //do nothing
                }
            }
            System.out.println("The number of vowels is "+ add + " in" + " : " + nextElement.toUpperCase());
    }