java 如何计算Java中字符串中的辅音数量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25262270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to count the number of consonants in a String in Java?
提问by user3898380
I am writing a program that counts the number of vowels and consonants from a sentence entered by the user. My code below counts the number of vowels but it give me strange numbers for consonant counts. For example if i enter "g" I get consonant count being 10.
我正在编写一个程序来计算用户输入的句子中元音和辅音的数量。我下面的代码计算元音的数量,但它给了我奇怪的辅音计数数字。例如,如果我输入“g”,我得到的辅音数为 10。
import java.util.Scanner;
public class VowelCount{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.print("Enter a sentence :");
String sentence = scan.nextLine();
String vowels = "aeiouAEIOU";
int vowelCount = 0;
int consCount = 0;
int i;
for(i = 0; i < sentence.length(); i += 1){
char currentChar = sentence.charAt(i);
int index;
for(index = 0; index < vowels.length(); index += 1){
if(vowels.charAt(index) == (currentChar)){
vowelCount++;
}else if(Character.isLetter(currentChar) && (vowels.charAt(index) == (currentChar))){
consCount++;
}
}
}
System.out.println(consCount);
System.out.println(vowelCount);
}
}
回答by sina72
This works. I have also improved it, see vowels.indexOf
(instead of manual iteration) and the line with isLetter
(corrected wrong check on vowels) and output (added ;
) and i++
.
这有效。我也对其进行了改进,请参阅vowels.indexOf
(而不是手动迭代)和isLetter
(更正元音错误检查)和输出(添加;
)和i++
.
public class VowelCount{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.print("Enter a sentence :");
String sentence = scan.nextLine();
String vowels = "aeiouAEIOU";
int vowelCount = 0;
int consCount = 0;
int i;
int length = sentence.length();
for(i = 0; i < length; i ++){
char currentChar = sentence.charAt(i);
if (vowels.indexOf(currentChar)>=0)
vowelCount++;
else if(Character.isLetter(currentChar))
consCount++;
}
System.out.print(consCount+";");
System.out.print(vowelCount);
}
}
回答by Abhishek Sarkar
This is the final solution guys :)
这是最终的解决方案伙计们:)
import java.util.Scanner;
public class VowelConsCount
{ public static void main(String args[])
{ System.out.print("Enter a sentence :");
Scanner in = new Scanner(System.in);
String sentence = in.nextLine();
String vowels = "aeiouAEIOU";
int vowelCount = 0;
int consCount = 0;
int i;
for(i = 0; i < sentence.length(); i ++)
{ char currentChar = sentence.charAt(i);
if (vowels.indexOf(currentChar)>=0)
vowelCount++;
else if(Character.isLetter(currentChar))
consCount++;
}
System.out.print("\nNumber of vowels is "+vowelCount);
System.out.print("\nNumber of consonant is "+consCount);
}
}
回答by Barbe Rouge
In that line:
else if(Character.isLetter(currentChar) && (vowels.charAt(index) == (currentChar))){
you are checking if your char is a letter and also if it's a vowel. You could try that in the condition test:
在该行中:
else if(Character.isLetter(currentChar) && (vowels.charAt(index) == (currentChar))){
您正在检查您的字符是否是字母以及它是否是元音。您可以在条件测试中尝试:
else if(Character.isLetter(currentChar)){
回答by Niru
The problem is with else part of your inner for-loop.
问题在于内部 for 循环的 else 部分。
boolean isVowel(char c){
String vowels = "aeiouAEIOU";
for(int index = 0; index < vowels.length(); index += 1)
if(vowels.charAt(index) == c)
return true;
return false;
}
Now after you get the currentChar just try this
现在在你得到 currentChar 之后就试试这个
if(isVowel(currentChar))
vowelCount++;
else
constCount++;
回答by ortis
There is several issue with your code:
您的代码有几个问题:
- You are using
System.out.print
instead ofSystem.out.println
which makes reading number misleading. You are looping over possible vowels and incrementing
vowelCount
if vowel is found but incrementing consCount if not !. You should split the two: first checking vowels, then consonants.Scanner scan = new Scanner(System.in); System.out.print("Enter a sentence :"); String sentence = scan.nextLine(); String vowels = "aeiouAEIOU"; int vowelCount = 0; int consCount = 0; int i; System.out.println("["+sentence+"]"); for (i = 0; i < sentence.length(); i += 1) { char currentChar = sentence.charAt(i); boolean isVowel = false; int index; for (index = 0; index < vowels.length(); index += 1) { if (vowels.charAt(index) == (currentChar)) { vowelCount++; isVowel =true; break;//we found the vowel, no need to keep looping } } if (!isVowel && Character.isLetter(currentChar))//vowel have not been found { consCount++; } } System.out.println("cons: " + consCount); System.out.println("vowel: " + vowelCount);
- 您正在使用
System.out.print
而不是System.out.println
which 使阅读数字具有误导性。 您正在遍历可能的元音并
vowelCount
在找到元音时递增,如果未找到则递增 consCount!. 您应该将两者分开:首先检查元音,然后检查辅音。Scanner scan = new Scanner(System.in); System.out.print("Enter a sentence :"); String sentence = scan.nextLine(); String vowels = "aeiouAEIOU"; int vowelCount = 0; int consCount = 0; int i; System.out.println("["+sentence+"]"); for (i = 0; i < sentence.length(); i += 1) { char currentChar = sentence.charAt(i); boolean isVowel = false; int index; for (index = 0; index < vowels.length(); index += 1) { if (vowels.charAt(index) == (currentChar)) { vowelCount++; isVowel =true; break;//we found the vowel, no need to keep looping } } if (!isVowel && Character.isLetter(currentChar))//vowel have not been found { consCount++; } } System.out.println("cons: " + consCount); System.out.println("vowel: " + vowelCount);
回答by Saif
Use this:
用这个:
Scanner scan = new Scanner(System.in);
System.out.print("Enter a sentence :");
String sentence = scan.nextLine();
String vowels = "aeiouAEIOU";
int vowelCount = 0;
int consCount = 0;
int i;
boolean flag;
for(i = 0; i < sentence.length(); i += 1){
char currentChar = sentence.charAt(i);
flag=false;
int index;
for(index = 0; index < vowels.length(); index += 1){
if(vowels.charAt(index) == (currentChar)){
vowelCount++;
flag=true;
continue;
}
}
if(!flag && Character.isLetter(currentChar) ){
consCount++;
}
}
System.out.println(consCount);
System.out.println(vowelCount);
changes
变化
What you were doing :
你在做什么 :
for(index = 0; index < vowels.length(); index += 1){
if(vowels.charAt(index) == (currentChar)){
vowelCount++;
}else if(Character.isLetter(currentChar) && (vowels.charAt(index) == (currentChar))){
consCount++;
}
in there the consCount
getting increased every time it doesn't match with a vowel.
for example if you try hello
the consCount
will be increased to 10 for only h
as it doesn't math with any vowels.
在那里consCount
每次与元音不匹配时都会增加。例如,如果你尝试hello
了consCount
将增加至10只h
与任何元音,因为它没有数学。
so what i did here is i put the consCount++
outside the 2nd for loop and add an flag
to check whether its an vowel or not. If its an vowel then consCount++
will be skipped and if not then it will be checked for latter the considered as an consonant.
所以我在这里做的是我把consCount++
外面的第二个 for 循环并添加一个flag
来检查它是否是元音。如果它是元音, consCount++
则将被跳过,如果不是,则将检查后者,将其视为辅音。
回答by NorwegianStudent
public static void main(String[] args) {
Scanner stringin = new Scanner(System.in);
String string1;
System.out.println("Enter a string: ");
string1 = stringin.nextLine();
string1 = string1.toLowerCase();
int count = 0;
int vowels = 0;
int consonants = 0;
int spaces = 0;
for (int i = 0; i < string1.length(); i++) {
char letter = string1.charAt(i);
if (letter == 'a' || letter == 'e' ||
letter == 'i' || letter == 'o' ||
letter == 'u')
{
vowels++;
}
else if (letter == ' ')
{
spaces++;
}
else
{
consonants++;
}
}
System.out.print("The number of vowels is " + vowels);
System.out.print(" The number of consonants is " + consonants);
}
Here is my solution. I had to make a exception for space as it got counted as a consonant. Currently taking a class in Java, so I am not experienced at all.
这是我的解决方案。我不得不为空格做一个例外,因为它被算作辅音。目前正在学习 Java 课程,所以我完全没有经验。