计算字符串中的所有元音

问题描述 投票:0回答:6

我正在开发一个计算字符串中元音数量的程序,但出现错误:

import java.util.Scanner;
public class Main
{
    public static void main(String[] args) {
        int vowels = 0;
        
        
        Scanner input = new Scanner(System.in);
        System.out.println("Enter String: ");
        String string = input.nextLine(); 

        string=string.toLowerCase();
        System.out.println("Vowels: "+ vowels);
    }
    public static void countVowels(String string)
    {
        int i;
        for (i=0;i<string.length();i++)
        {
            if (string.charAt(i) = "a" || string.charAt(i) = "e" || string.charAt(i) = "i" || string.charAt(i) = "o" || string.charAt(v) = "u")
            {
                vowels++; 
            }
        }
    }
}

 

哦,我想问一下没有返回值/有返回值的方法意味着什么。不确定上面的代码是否有返回值。

java methods
6个回答
1
投票

我上面的代码有很多问题。

首先,主类甚至没有调用方法类

(countVowels)

其次,我使用双引号

" "
并将其称为
char
。字符串使用
" "
,字符使用
' '

第三,我使用单个

=
而不是
==

下面是精炼代码

import java.util.Scanner;
public class Main
{
    public static void main(String[] args) {
     
        Scanner input = new Scanner(System.in);
        System.out.println("Enter String: ");
        String string = input.nextLine(); 

        string=string.toLowerCase();
        countVowels(string);
        
    }
    public static void countVowels(String string) //method class, dito yung parang procedure nung pagbibilang ng vowels
    {
        int v;
        int vowels = 0;
        for (v=0;v<string.length();v++)
        {
            if (string.charAt(v) == 'a' || string.charAt(v) == 'e' || string.charAt(v) == 'i' || string.charAt(v) == 'o' || string.charAt(v) == 'u')
            {
                vowels++; 
            }
            
        }
        System.out.println("Vowels: "+ vowels);
        
        
    }
}

0
投票

你的代码中有很多错误。

在 Java 中,用单引号表示一个字符,例如 'a' 'b' 等。

字符串是双引号,是一个对象“a”,“b”。

您无法将字符与字符串进行比较。

您需要将 char 与 char 进行比较。

string.charAt(i) == 'a'

并且缺少双等号。


0
投票

您需要解决以下问题,

  1. 变量
    vowels
    的范围设置为方法级别,因此在
    main
    方法
  2. 之外不可用
  3. 方法
    countVowels
    永远不会被调用
  4. 赋值运算符
    =
    用于代替比较
    ==
  5. 使用字符串文字
    " "
    代替 char
    ' '

0
投票

大部分问题已经被其他人回答了,剩下的很少。
您没有调用您的函数

countVowels()
并且您已在
vowels
方法中声明了变量
main
,而不是在您的函数
countVowels()
中。
有返回值的方法:

import java.util.Scanner;
public class Main
{
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter String: ");
        String string = input.nextLine(); 
        string=string.toLowerCase();
        int vowels = countVowels(string); // calling function which returns a value.
        System.out.println("Vowels: "+ vowels);
    }
    public static int countVowels(String string)
    {
        int i,vowels=0;
        for (i=0;i<string.length();i++)
        {
            if (string.charAt(i) == 'a' || string.charAt(i) == 'e' || string.charAt(i) == 'i' || string.charAt(i) == 'o' || string.charAt(i) == 'u')
            {
                vowels++; 
            }
        }
        return vowels;
    }
}

无返回值的方法:

import java.util.Scanner;
public class Main
{
    public static void main(String[] args) {
        .... // same code as above
        countVowels(string);  // calling funtion, this doesnt return value      
    }
    public static void countVowels(String string)
    {
        int i,vowels=0;
        for (i=0;i<string.length();i++)
        {
            .... // same code as above
        }
        System.out.println("Vowels: "+ vowels);
    }
}

0
投票

如果您对替代方法感兴趣,请尝试这个。

  • 一次传输一个要处理的字符。
  • 过滤元音字符串中包含的那些。
  • 并计算那些通过的。
String str = "To be or not to be, that is the question.";
long count = str.chars().filter(ch->"aeiou".contains((char)ch+"")).count(); 
System.out.println(count);

打印

13

0
投票
    public static int  countVowelConsonants(String inpStr) {
    Map<Character,Integer> inpCount = new LinkedHashMap<>();
    int totalVowel =0;
    for ( char c : inpStr.toCharArray())
    {
        inpCount.put(c,inpCount.containsKey(c)?inpCount.get(c)+1:1);
    }

    for (Map.Entry<Character,Integer> iterator : inpCount.entrySet())
    {
        if (iterator.getKey()=='a'||iterator.getKey()=='e'||iterator.getKey()=='i'||iterator.getKey()=='o'||iterator.getKey()=='u')
        {
            totalVowel = totalVowel + iterator.getValue();

    }
    }
    return totalVowel;
}
© www.soinside.com 2019 - 2024. All rights reserved.