Java 如何在使用方法输出时计算元音和辅音并将字符串中的第一个字母大写并返回结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19661199/
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 vowels and consonants and capitalizing first letter in a string while output using a method and return result
提问by Edrick
If a user enters a string: hello there
如果用户输入一个字符串: hello there
it should output
它应该输出
Hello has 2 vowels
There has 3 consonants.
I know this is a fairly simple code but I am getting too many ideas and getting confused. I need a to make sure I have 2 methods for numberofVowels and capitalizeWord and both returns a result
我知道这是一个相当简单的代码,但我有太多的想法并且感到困惑。我需要一个来确保我有 2 个 numberofVowels 和 capitalizeWord 方法,并且都返回一个结果
I am getting an error and I am still trying to figure out to capitalize after I got counting vowels work
我遇到了一个错误,在计算元音后,我仍在试图弄清楚如何使用大写字母
import java.util.Scanner;
public class Hwk9
{
public static void main (String args[])
{
Scanner stdin = new Scanner(System.in);
String string1;
System.out.println("Enter a string");
string1 = stdin.nextLine();
string1 = string1.toLowerCase();
}
public static int numberVowels(String string1)
{
int count = 0;
int vowels = 0;
int consonants = 0;
for (int i = 0; i < string1.length(); i++)
{
char ch = string1.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i' ||
ch == 'o' || ch == 'u')
{
vowels++;
}
else
{
consonants++;
}
}
}
}
回答by kerberos84
I suggest you;
我建议你;
- to create a final vowels array,
- define a
bool isVowel(char c)
function and use it in your if condition.
- 创建一个最终元音数组,
- 定义一个
bool isVowel(char c)
函数并在你的 if 条件中使用它。
回答by Shamse Alam
following code will give you vowel and Constonent count
以下代码将为您提供元音和辅音计数
static String VOWEL_GROUP = "AEIOUaeiou";
static String testString = "AAAASHMAIOUAXCCDIOUGGGGA"; // say this is your text
public static void main(String[] args) {
int vovelCount = 0;
int consonantCount = 0;
for (int j = testString.length() - 1; j >= 0; j--) {//outer loop
for (int i = 0; i < VOWEL_GROUP.length(); i++) { //inner loop
if (VOWEL_GROUP.charAt(i) == testString.charAt(j)) {
vovelCount++; //vowel count in text
break;
}else{
consonantCount ++;
}
}
}
System.out.println(vovelCount+" "+ consonantCount);
}
回答by Bharath R
Made something like this hope this helps, this will give vowels,consonants of each word
做了这样的事情希望这会有所帮助,这将给出每个单词的元音和辅音
public static void main (String args[])
{
Scanner stdin = new Scanner(System.in);
String string1;
System.out.println("Enter a string");
string1 = stdin.nextLine();
string1 = string1.toLowerCase();
int count = 0;
int vowels = 0;
int consonants = 0;
for (String retval: string1.split(" ")){
for (int i = 0; i < retval.length(); i++)
{
char ch = retval.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i' ||
ch == 'o' || ch == 'u')
{
vowels++;
}
else
{
consonants++;
}
}
System.out.println(retval.substring(0, 1).toUpperCase() + retval.substring(1)+" has "+vowels+" vowels and "+consonants+" cosonants");
vowels=0;
consonants=0;
}
}
回答by Actiwitty
Here is a simpler way of doing this, hope this helps:
这是一种更简单的方法,希望对您有所帮助:
public static void checkVowels(String s){
System.out.println("Vowel Count: " + (s.length() - s.toLowerCase().replaceAll("a|e|i|o|u|", "").length()));
//Also eliminating spaces, if any for the consonant count
System.out.println("Consonant Count: " + (s.toLowerCase().replaceAll("a|e|i|o| |u", "").length()));
}
回答by sunp
Here is the simple code for counting the number of vowels using recursion
这是使用递归计算元音数量的简单代码
public static int vowels(String s){
int count =0;
char c;
if(s.length()==0){
return 0;
}
else{
c =s.charAt(0);
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'){
count++;
}
return count+vowels(s.substring(1));
}
}
回答by Kaberi Mishra
import java.util.Scanner;
//don't use space in between the input string;
class StrRev {
static String Vowels ="aeiouAEIOU";
public static void main(String[] args){
@SuppressWarnings("resource")
Scanner name = new Scanner(System.in);
String nm = name.nextLine();
System.out.println();
int vowel=0;
int consonant=0;
for(int i=0;i<nm.length();i++){
for(int j=0;j<Vowels.length();j++){
if(Vowels.charAt(j)==nm.charAt(i)){
vowel++;
break;
}
}
}
consonant = nm.length()-vowel;
System.out.println("no of Vowels :"+vowel+"\nno of consonant :"+consonant);
}
}
回答by ZolkiBy
My code without scanner and maybe not very simple, but:
我的代码没有扫描仪,也许不是很简单,但是:
public class Main{
public static void main(String[] args) {
String test = "qwertyYVTA";
test = test.trim();
int vowels = 0;
int consonants = 0;
Pattern patternVow = Pattern.compile("[eyuioa]", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
Matcher matcherVow = patternVow.matcher(test);
while (matcherVow.find()) {
vowels++;
}
Pattern patternCons = Pattern.compile("[wrtpsdfghjklzxcvbnm]", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
Matcher matcherCons = patternCons.matcher(test);
while (matcherCons.find()) {
consonants++;
}
System.out.println("Vowels in test String is " + vowels);
System.out.println("Consonants in test String is " + consonants);
}
}
回答by user7420004
This looks way simple than above answers. It gets the input, converts it to lowercase then to an array of characters. A simple for loop will do the trick onwards.
这看起来比上面的答案简单。它获取输入,将其转换为小写,然后转换为字符数组。一个简单的 for 循环就可以解决问题。
import java.util.*;
public class FindVowelsConsonents {
public static void main(String[] args) {
int vowels_count = 0;
int consonents_count = 0;
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String str2 = str.toLowerCase();
char[] chr = str2.toCharArray();
for(int i=0;i<chr.length;i++){
if(chr[i] == 'a' || chr[i]== 'e' || chr[i] == 'i' ||
chr[i] == 'o' || chr[i] == 'u')
vowels_count++;
else
consonents_count++;
}
System.out.println(vowels_count+ " "+consonents_count);
sc.close();
}
}
回答by Filip
As far as I am concerned you can use StringTokenizer:
就我而言,您可以使用 StringTokenizer:
String text = "dupalo twoja mama";
StringTokenizer tokenizer = new StringTokenizer(text,"aeiuo",false);
int vowels = tokenizer.countTokens();
System.out.println(vowels);
In this case of "text" it will print out 7.
在这种“文本”的情况下,它将打印出 7。