Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
计算Java中字符串中的元音数_Java_Arrays_If Statement - Fatal编程技术网

计算Java中字符串中的元音数

计算Java中字符串中的元音数,java,arrays,if-statement,Java,Arrays,If Statement,我试图创建一个方法来计算字符串中的元音数,但我的代码中有一个我无法识别的缺陷 例如,如果我输入单词fire,它会打印出: 元音:uVowels:e 我不知道为什么不打印元音的数量。这是我的密码: import java.util.Scanner; public class Counter { // class variables shared by more than one method String prompt; static String strUserResp

我试图创建一个方法来计算字符串中的元音数,但我的代码中有一个我无法识别的缺陷

例如,如果我输入单词fire,它会打印出:

元音:uVowels:e

我不知道为什么不打印元音的数量。这是我的密码:

import java.util.Scanner;
public class Counter
{ 
    // class variables shared by more than one method
    String prompt;
    static String strUserResponse;

    // main method
    public static void main (String[] args)
    {
        vowelCounter();
    }
public static void vowelCounter()
{
    for (int i = 0; i<strUserResponse.length();i++)
    {
        char v = strUserResponse.charAt(i);
         if (v == 'a' || v == 'e' || v == 'i' || v == 'o' || v == 'u'
         || v == 'A'|| v == 'E' || v == 'I' || v == 'O' || v == 'U')
        {
            System.out.print ("Vowels: " + v);
        }
   }
import java.util.Scanner;
公共课柜台
{ 
//由多个方法共享的类变量
字符串提示;
静态串级响应;
//主要方法
公共静态void main(字符串[]args)
{
元音计数器();
}
公共静态void元音计数器()
{

对于(int i=0;i你没有计算人声。你的变量v是一个字符。你应该有一个acumulator并增加它

int numberOfVowels = 0;
for (int i = 0; i<strUserResponse.length();i++){
        char v = strUserResponse.charAt(i);
         if (v == 'a' || v == 'e' || v == 'i' || v == 'o' || v == 'u'
         || v == 'A'|| v == 'E' || v == 'I' || v == 'O' || v == 'U')
        {
            System.out.println ("Vowels: " + v); //just print the vocal
            numberOfVowels++; //increment numberOfVowels
        }
}

System.out.println ("Total of Vowels is: " + numberOfVowels);
int numberOfVowels=0;
for(int i=0;i它会多次打印“元音”,因为for循环中有“元音”,所以每次if语句为true时,它都会将“元音”与所需的元音一起打印。您可以在开始for循环之前打印“元音”以解决此问题(注意,这是一个最好的假设,假设您正在检查的任何内容都将包含元音,否则它将只打印“元音:”而没有任何尾随)

您还忘了声明一个变量来跟踪元音的数量。因为您已经在检查打印看到的元音,所以在打印每个循环出现的元音时,您应该添加到该计数器中

public static void vowelCounter(){
    int numVowels = 0;
    System.out.print("Vowels:")
    for (int i = 0; i<strUserResponse.length();i++){
        char v = strUserResponse.charAt(i);
        if (v == 'a' || v == 'e' || v == 'i' || v == 'o' || v == 'u'
          || v == 'A'|| v == 'E' || v == 'I' || v == 'O' || v == 'U'){
            System.out.print(" "+v);
            numVowels++;
        }
    }System.out.print("Number of vowels: "+numVowels);
}
公共静态void元音计数器(){
int numowels=0;
System.out.print(“元音:”)

对于(int i=0;i,因此我尝试保持代码的格式和一般结构不变(使用一个名为Counter的类)。下面是一种非常简单的方法(尽管保持类名计数器似乎有点不稳定)

import java.util.Scanner;
公共课柜台
{
//由多个方法共享的类变量
公共字符串word;//要检查元音的单词
私有静态final char[]元音={'a','e','i','o','u'};//要检查的元音的char[]
/*字符串提示;
静态串级响应*/
//主要方法
公共静态void main(字符串[]args)
{
Counter word=new Counter();//创建计数器类的新实例
word.word=args[0];//设置计数器变量的字
System.out.println(word.vowells());//打印word中元音的数量
}
/*返回单词中元音的数量*/
公共int元音()
{
String lowerWord=this.word.toLowerCase();//将单词设置为小写,以便能够检查大写元音
整数计数=0;
if(word.length()>0){
for(int i=0;i
Y
感到孤独……但是,好吧,实际上你什么都不算。只是打印元音。@Tunaki有时…;-)无法重现:1)问题不是(它不完整,所以无法验证)。2)打印
i
e
时发出
fire
,因此不知道你的
u
来自何处。我想你的意思是“不是”。谢谢,我不懂英语。我终于明白我做错了什么并修复了它。谢谢!
import java.util.Scanner;
public class Counter
{
  // class variables shared by more than one method
  public String word; // Word to be check for vowels
  private static final char[] vowels = {'a', 'e', 'i', 'o', 'u'}; // char[] of vowels to be checked against


  /*String prompt;
  static String strUserResponse;*/

  // main method
  public static void main (String[] args)
  {
    Counter word = new Counter(); // Create new instance of Counter class
    word.word = args[0]; // Set the word of the Counter variable
    System.out.println(word.vowels()); // Print nubmer of vowels in word
  }
  /* Returns the nubmer of vowels in the word */
  public int vowels()
  {
    String lowerWord = this.word.toLowerCase(); // Make word lowercase to be able to check for uppercase vowels, too
    int count = 0;
    if (word.length() > 0) {
      for (int i = 0; i < word.length(); i++) {
        count += (this.isVowel(lowerWord.charAt(i)) ? 1 : 0); // if the letter in the current iteration for the loop is a vowel, increase the count, otherwise, don't
      }
    }
    return count;
  }

  private static boolean isVowel(char c) {
    for (char ch : vowels) {
      // If the character is a vowel, return true
      if (c == ch) return true;
      else continue; // continue so if the char is not a vowel it doesn't just return false
    }
    return false; // if no vowels are found, return false
  }
 }