我正在尝试获取以元音开头和结尾的数组中最长的字符串。当我运行我的代码时,每个循环后显示最长的值,但它不会显示变量最长的最高值。
class xJava
{
public static void firstlastVowel (String theString)
{
int index;
int longest=0;
char x = theString.charAt(index=0);
char y = theString.charAt(theString.length()-1);
int z = theString.length()-1;
if(x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u')
{
if(y == 'a' || y == 'e' || y == 'i' || y == 'o' || y == 'u')
{
System.out.println(theString + " starts and ends with a vowel");
if(z > longest)
{
longest = z;
System.out.println("longest string is "+longest+" characters!");
}
}
}
}
public static void main (String[] args)
{
int index;
String value;
String[] things = {"abba", "orlando", "academia", "ape"};
for(String thing : things)
{
firstlastVowel(thing);
}
}
}
如何让变量最长只包含最长字符串的长度?
输出是:
abba starts and ends with a vowel
longest string is 3 characters!
orlando starts and ends with a vowel
longest string is 6 characters!
academia starts and ends with a vowel
longest string is 7 characters!
ape starts and ends with a vowel
longest string is 2 characters!
答案 0 :(得分:2)
Gah,我应该知道比发布这个更好:
String[] things = { "aa", "orrro", "academia", "ape" };
int longest = Arrays.stream(things)
.filter(s -> s.matches("^[aeiouy].*[aeiouy]$"))
.map(String::length)
.reduce(0, Math::max);
System.out.println("longest string is " + longest + " characters!");
为什么以及如何运作留给读者练习。
答案 1 :(得分:0)
这是问题 -
offset = lines.count()
new_indexed_lines = (new_lines
.zipWithIndex()
.map(lambda xi: (xi[1] + offset, xi[0])))
变量的方法作用于longest
,并且每次调用firstlastVowel
方法时都会重置为零。
在firstlastVowel
方法中对其进行初始化,并将其作为参数传递给main
以及firstlastVowel
,并按顺序使用thing
类型而不是Integer
保持此变量的多次传递与int
方法之间的引用。
答案 2 :(得分:0)
这可能是您需要的一个简单示例:
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String[] things = {"abba", "orlando", "academia", "ape", "nebuchadnezzar", "academi","academian"};
String longestWord = getLongestVowelWord(things);
System.out.println("The longest vowel word is: " + longestWord);
}
/**
* Method returning the String which starts and ends with a vowel and is the longest
* within the Strings given as an array in this method's parameter
* @param words String array containing words to be examined by the method
* @return String object which starts and ends with a vowel and is the longest from
* the words given as a String array
*/
private static String getLongestVowelWord(String[] words){
String longestWord = "";
for(String word : words){
if(longestWord.length() < getVowelWordLength(word)){
longestWord = word;
}
}
return longestWord;
}
/**
* Method check if word starts and ends with a vowel
* and returning it's length it case it matches the pattern
* @param word a word whose length is being checked on condition that it starts and ends with a vowel
* @return if word starts and ends with a vowel, returns it's lenght. Returns -1 otherwise.
*/
private static Integer getVowelWordLength(String word){
// check if first and last char of the word is a vowel
// toLowerCase() is used for simplicity of isVowel(char c) method - it does not have to check the upper case chars
if(isVowel(word.toLowerCase().charAt(0))
&& isVowel(word.toLowerCase().charAt(word.length()-1)))
return word.length();
else
return -1;
}
/**
* Method checking if given char is a vowel
* @param c char being checked for being a vowel
* @return <code>true</code> if given char is a vowel, <code>false</code> otherwise
*/
private static boolean isVowel(char c){
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}