- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
如果用户输入字符串:hello there
它应该输出
Hello has 2 vowels
There has 3 consonants.
我知道这是一个相当简单的代码,但我有太多的想法并且感到困惑。我需要一个来确保我有 2 个方法用于 numberofVowels 和 capitalizeWord 并且都返回一个结果
我遇到了一个错误,在我计算元音后,我仍在尝试弄清楚如何利用
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++;
}
}
}
}
最佳答案
这是一个更简单的方法,希望对您有所帮助:
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()));
}
关于java - 如何在使用方法输出并返回结果时计算字符串中的元音和辅音并将首字母大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48693023/
我试图在 haskell 上创建一个函数,我知道该函数的定义如下: justWithA : [Char] -> Bool justWithA [] = True justWithA (x:xs) |
我试图在 haskell 上创建一个函数,我知道该函数的定义如下: justWithA : [Char] -> Bool justWithA [] = True justWithA (x:xs) |
我正在尝试计算字符串中元音的总数。我正在使用 strlen 来获取字符串的总长度,但是当我尝试按每个字母对字符串进行计数时,它说 C++ 禁止比较。所以我假设我的 if 语句有问题。 #include
Task You are given a string . It consists of alphanumeric characters, spaces and symbols(+,-). Your
我刚刚开始用 C 语言编程,我必须创建一个程序来计算字符串有多少个元音。到目前为止我有这个: int a; int len = strlen(text)-1 for(a=0;a==len;++a){
尝试学习 Python 中的正则表达式以查找具有连续元音-辅音或辅音-元音组合的单词。我将如何在正则表达式中执行此操作?如果无法在 Regex 中完成,是否有一种在 Python 中执行此操作的有效方
我想输入 dagger-alif、dagger-waw 和 dagger-ya(也称为微型 alif、微型哇和微型 ya)作为 alif-wasla 使用 PC 阿拉伯语键盘。这些标记被使用帮助读者发
我正在尝试创建一个将文件上传到 FTP 服务器的批处理文件。除了一个特定文件夹的名称中包含突变/元音外,一切正常(无法更改。也就是文件夹名称中包含 ö。)。 我的问题是:有哪些选择可以实现这一目标?
我是一名优秀的程序员,十分优秀!