Java 创造一种决定单词元音和谐的方法 公共静态字符串元音RMONY(字符串输入){ 字符串[]高={“e”,“i”}; 字符串[]deep={a”,“o”,“u”}; 对于(int i=0;i
我知道,我知道,英语中不存在元音和声,但是为了这个例子,让我们假设它存在。Java 创造一种决定单词元音和谐的方法 公共静态字符串元音RMONY(字符串输入){ 字符串[]高={“e”,“i”}; 字符串[]deep={a”,“o”,“u”}; 对于(int i=0;i,java,string,Java,String,我知道,我知道,英语中不存在元音和声,但是为了这个例子,让我们假设它存在。high元音是“e”和“i”。deep元音是“a”、“o”和“u”。所有单词属于high、deep或mixed组 例如: 如果一个单词只有high元音,那么它就是high单词(地狱、山、磨坊、杀戮等) 如果一个单词只有deep元音,那么它就是deep单词(剑、握、凳子、酷等) 如果一个单词的元音来自两组,那么它是一个混合词(mule、mountain、house、choose等) 唯一的问题是,我的代码没有按预期工作。
high
元音是“e”和“i”。deep
元音是“a”、“o”和“u”。所有单词属于high
、deep
或mixed
组
例如:
- 如果一个单词只有
元音,那么它就是high
单词(地狱、山、磨坊、杀戮等)high
- 如果一个单词只有
元音,那么它就是deep
单词(剑、握、凳子、酷等)deep
- 如果一个单词的元音来自两组,那么它是一个混合词(mule、mountain、house、choose等)
唯一的问题是,我的代码没有按预期工作。如果单词是
混合的
,则不会显示。如果一个单词中甚至只有一个大写字母,它将显示为high
。我需要做什么来修复它?我的代码有什么问题吗?您可以很容易地做到:
public static String vowelHarmony(String input) {
String[] high = {"e", "i"};
String[] deep = {"a", "o", "u"};
for (int i = 0; i < input.length(); i++) {
if (input.contains(high[i])&&!input.contains(deep[i])){
return "high";
}
else if (input.contains(deep[i])&&!input.contains(high[i])){
return "deep";
}
else if (input.contains(deep[i])&&input.contains(high[i])){
return "mixed";
}
}
return "you screwed something up";
}
上述代码存在两个问题:
- 一旦在第一次出现时满足任何条件(这就是它检查的全部内容-第一次出现),您将得到结果
- 如果您的输入长度超过两个字母数组中的任何一个(确实如此),您将获得一个
ArrayIndexOutOfBoundsException
enum Harmony {
Deep, High, Mixed, Nothing
}
public static Harmony vowelHarmony(String input) {
boolean canBeHigh = false, canBeDeep = false;
if (input.contains("a") || input.contains("o") || input.contains("u"))
canBeDeep = true;
if (input.contains("e") || input.contains("i"))
canBeHigh = true;
if (canBeDeep && canBeHigh)
return Harmony.Mixed;
if (canBeDeep)
return Harmony.Deep;
if (canBeHigh)
return Harmony.High;
return Harmony.Nothing;
}
然后你可以在你的方法中检查它。还要注意不要立即从方法返回
private static boolean hasHighVowel(String input) {
return input.contains("e") || input.contains("i");
}
private static boolean hasLowVowel(String input) {
return input.contains("a") || input.contains("o") || input.contains("u");
}
错误处理案例——例如当用户在该方法中输入
null
或空字符串时——留给读者作为练习。嗨,我做了一些工作,只是为了让生活变得简单。不过我知道这不是一个好的编码实践
public static String vowelHarmony(String input) {
String result = "you screwed something up";
if (hasHighVowel(input)) {
result = "high";
}
if (hasLowVowel(input)) {
result = "deep";
}
if (hasHighVowel(input) && hasLowVowel(input)) {
result = "mixed";
}
return result;
}
一旦您的任何return语句被命中,该方法就会结束。例如,如果
hello
是您的单词,它将检查e
和a
(每个数组上的第一个位置),这两个位置都是true
,因此它将返回high
,不再搜索。另外,在两个不同长度的数组上使用输入的长度进行搜索可能并不理想。三个布尔值,每个值都附加了多级条件。读起来并不是那么简单。这不能处理没有元音的输入情况。为什么要使用&&
然后否定每个术语isHigh=input.contains(“e”)|| input.contains(“i”)代码>对我来说更有意义。我更改它是为了更好地查看。它不能处理不是两个的情况(不深也不高)。谢谢您的提醒。您仍然在使用&&
而不是|
,但是使用枚举而不是字符串会获得额外的积分。我们谈论的是Java,而不是Perl。不需要严格键入值。谢谢。我也修好了。我使用Enum只是为了更好的表达和清晰。谢谢,这是我最喜欢的解决方案,也是最具可读性的。我不知道为什么我没有想到用其他方法来检查不同类型的元音。
enum Harmony
{
High,Low,Mixed,Screwed
}
public static Harmony vowelHarmony(String input)
{
String[] high = {"e", "i"};
String[] deep = {"a", "o", "u"};
input=input.replaceAll("[ei]", "1");
input=input.replaceAll("[aou]", "0");
input=input.replaceAll("[^01]", "");
if(input.contains("1") && !input.contains("0"))
return Harmony.High;
else if(input.contains("1") && !input.contains("0"))
return Harmony.Low;
else if(input.contains("1") && !input.contains("0"))
return Harmony.Mixed;
else
return Harmony.Screwed;
}