- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
计算单词中元音的方法
public class Methods6 {
public static int countVowel(String words) {
int count = 0;
char[] vowel = {'a', 'e', 'i', 'o', 'u'};
for (int i = 0; i < words.length(); i++) {
char ch = words.charAt(i);
for (char cc : vowel) {
if (ch == cc) {
count++;
}
}
}
return count;
}
**找到句子中最大的元音**
public static String maxVowelWords() {
String sentence = getSentence().toLowerCase();
String words[] = sentence.split(" ");
int maxvowel = CountVowel(words[0]), count;
String maxWord = words[0];
for (int i = 1; i < words.length; i++) {
count = CountVowel(words[i]);
if (count > maxvowel) {
maxvowel = count;
maxWord = words[i] + " ";
}
}
return maxWord;
}}// 2 methods are located in the same Method Class
public class Test {
public static void main(String[] args) {
System.out.println(Methods6.MaxVowelWords());}}
编写此方法时,它仅给出元音字母最多的第一个单词(例如 --------- 你好我的 friend ! ----------- 只是你好,你好, friend ——不!我怎样才能改变这个方法?感谢您的帮助!
最佳答案
我对这个问题的解释是:
因此,如果提供的字符串是:“hello myfriend”
,那么两个单词 hello
和 friend
都将从maxVowelWords() 方法的形式如下(比方说):
hello, friend
由于 hello
和 friend
都包含 2 个元音,这恰好是任何给定单词中元音的最大数量,因此返回两个单词。
如果提供的字符串是:“你好,我的 friend - 我住在密西西比河上。”
则该方法仅返回 Mississippi,因为它是 <该字符串中唯一的单词最多包含 4 个元音。字符串中包含元音的所有其他单词都包含较小的元音计数。
如果情况确实如此,那么您的方法应该如下所示:
public static String maxVowelWords() {
String sentence = getSentence();
String words[] = sentence.split(" ");
int maxvowel = 0;
String maxWord = "";
for (int i = 0; i < words.length; i++) {
int count = countVowels(words[i]);
if (count == maxvowel) {
maxvowel = count;
// Ternary Operator is used here to applt the delimiter (", ") if needed,
maxWord += (maxWord.equals("") ? words[i] : ", " + words[i]) + " (" + count + " vowels)";
}
else if(count > maxvowel) {
maxvowel = count;
maxWord = words[i] + " (" + count + " vowels)";;
}
}
return maxWord;
}
是的,您从 0 开始循环,并且 maxWord 字符串变量应该初始化为保存空字符串 ("")。让for循环完成它的工作。不,计数并不是一种浪费,它实际上是一个要求,以便在通过该过程放置每个字符串单词时获得maxvowel的最大元音计数。
您使用名为 getSentence() 的方法来获取要处理的字符串,并立即通过将其设置为全部小写来扭曲该原始字符串。您确实不应该这样做,因为从 maxVowelWords() 方法返回的单词将不是最初提供的单词(如果它们碰巧具有大写元音)。对 countVowel() 方法进行小的修改就可以处理该事务,例如:
if (Character.toLowerCase(ch) == cc) {
count++;
}
关于java - 找到句子中元音最多的单词(可以是几个彼此等价的最大单词),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60596220/
看来我又被一个简单的正则表达式卡住了。 我想要什么: 1 到 999 之间的数字 可选:逗号、符号 如果输入逗号,最少1位小数,最多3位小数点应该是presebt。 Allowed: 100 999,
我需要从两列中获取最大值并将其发送到第三列。这些列的大小都是统一的,但有时大小会有所不同,但它们都将从同一个单元格开始。例如: 5 8 - 6 2 - 6 5 - 带有破折号的列需要找到其他两个之间的
我在我的网站上有一张包含用户排名列的表格,排名是一个数字,我想选择排名最高的 3 个用户,所以我查看并搜索了我认为最好的查询是那:Link (正确答案的第二个查询),但我不明白查询,如果有人能一步一步
我正在尝试制作一个点击计数器,我想收集 24 小时内的总点击次数。无论最终用户位于哪个时区,这 24 小时都应该是固定值。在 24 小时内,数据库应更新为 +1 次点击计数,一旦达到 24 小时时间范
我有一个在典型共享主机上运行的 PHP + MySQL Web 应用程序,我想知道调用最多的查询是什么以及消耗的资源量是多少。这样,我将专注于最昂贵的查询以优化资源或检测优化不佳的查询。 例如: qu
这是我“尝试”从用户输入的数字中找到最大 2 个值的代码: #include using namespace std; void maximum2(int a, int& max1,int& max
我需要编写一个 Python 函数,从具有最多“o”字符的字符串中返回单词。例如,get_most_ooo_word(['aa ao ooo']) 应该返回 ['ooo'] 和 get_most_oo
我正在寻找一种哈希算法,以创建尽可能接近字符串的唯一哈希值 (max len = 255),从而生成一个长整数 (DWORD)。 我意识到26^255 >> 2^32,但也知道英语的单词数远少于2^3
我得到了一个仅由 's','t','u','v' 作为字符组成的字符串 T。我想找到长度为 |T| 的字符串数它最多与 T 不同 n 个位置。而且每个这样的字符串在三个不同的位置不能有相同的字符,这些
我有一群“专家”(大约 300 人)可以胜任一项工作。而且我有很多工作必须完成,比如说大约 500 个。我也有信息,一个专家能做某项工作有多“好”。这将导致一个 300 x 500 的矩阵来保存权重。
我正在尝试解决这个问题,虽然我可以使用蛮力解决它,但是以下优化算法为我提供了一些测试用例的错误结果。我尝试了但无法找到代码的问题,任何人都可以帮助我。 问题:给定一个字符串 S 和整数 K,找到整数
我需要一个混合长度的正则表达式验证,总长度为 6 个字符,其中 4-6 个大写/数字字符和 0-2 个空格。 我试过 ^[A-Z0-9]{4,6}+[\s]{0,2}$ 但它导致最大长度为 8 个字符
我有一个数组 {-1,2,3,4,-3,-2,1,5} 现在我想找到给定数组的最小连续总和子数组,最多 K 次交换。 在上面的数组中,最小连续和是-5,子数组是{-3,-2} 对于 K=1 我应该如何
我们有一个简单的表格如下: ------------------------------------------------------------------------ | Name |
如果哈希不能超过 4 个字符,并且这 4 个字符只能是小写字母或数字,那么创建 String 哈希的最佳方法是什么? 我要散列的字符串有 1-255 个字符。我知道在没有冲突的情况下创建 4-char
我希望使用 Multipeer Connectivity 框架,并感谢任何关于如何最好地进行的经验之谈。 我需要在“教练”设备和最多 45 个“玩家”设备之间建立连接。他们都在同一个空间,但无法预测
给定一个数组 a,什么是实现其组合直到第 n 的最佳方法?例如: a = %i[a b c] n = 2 # Expected => [[], [:a], [:b], [:c], [:a, b], [
这个问题在这里已经有了答案: Formatting floats without trailing zeros (21 个回答) 关闭8年前。 我想格式化最多包含 2 个小数位的 float 列表。但
我无法使用以下形式的命令登录到远程 docker 注册表: docker login –u my-username –p my-password registry.myclient.com 我得到的错
所以这是我的代码:服务器.java import java.io.*; import java.net.*; import java.util.*; class Server implements R
我是一名优秀的程序员,十分优秀!