所以我正在编写代码来计算数量。句子中的元音和辅音。但这两种情况的输出都是 0。
我正在使用 removeAll();
删除空格、制表符、逗号或句号等。我无法发现我的代码有任何问题。
import java.util.*;
import java.lang.*;
public class VowelsConsonantsSentence
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Input The Sentence : ");
String in = sc.nextLine();
in = in.toLowerCase();
in = in.replaceAll("\\s+","");
in = in.replaceAll(".","");
in = in.replaceAll(",","");
int vCount=0, count=0;
for(int i=0;i<in.length();i++)
{
char x = in.charAt(i);
switch(x)
{
case 'a' :
case 'e' :
case 'i' :
case 'o' :
case 'u' : vCount++;
break;
default : count++;
}
}
System.out.println("\nNo. of vowels : "+vCount);
System.out.println("\nNo. of consonants : "+count);
}
}
变量 count
和 vCount
的计算结果均为 0。
最佳答案
replaceAll
将正则表达式作为输入,所以 "."
删除所有字符。
您可以删除所有 replaceAll
与 in = in.replaceAll("[^A-Za-z]+","");
它应该可以工作。
关于java - 查找句子中的元音和辅音的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52562384/