Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java—数组中最长的字符串,包含元音开头和结尾_Java_Arrays_Methods - Fatal编程技术网

java—数组中最长的字符串,包含元音开头和结尾

java—数组中最长的字符串,包含元音开头和结尾,java,arrays,methods,Java,Arrays,Methods,我试图获得数组中以元音开头和结尾的最长字符串。当我运行代码时,每个循环后都会显示最长的值,但它不会显示变量最长的最大值 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(

我试图获得数组中以元音开头和结尾的最长字符串。当我运行代码时,每个循环后都会显示最长的值,但它不会显示变量最长的最大值

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);
    }

}

}
如何使变量longest只包含最长字符串的长度

输出为:

 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!
问题就在这里-

您的
longest
变量是
firstLastVowell
中的方法作用域,并且每次调用
firstLastVowell
方法时都会重置为零


main
方法中初始化它,并将它作为参数与
对象一起传递给
firstlastVowel
,并使用
Integer
类型而不是
int
,以保持此变量多次传递到
firstlastvowell
方法之间的引用。

  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!");

为什么以及如何工作留给读者作为练习。

这可能是您需要的一个简单示例:

/**
 * @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';
}

你能添加你得到的输出吗?请考虑第一个ListVoWEL将被调用4次,一次在字符串数组中的每个单词。每次调用firstLast元音时,Longest都设置为0,并且它始终等于该字符串。最好在方法中为您的方法提供参数中的数组和循环,然后返回longestZ=theString.length()更好。您每次都会以其他方式漏掉一个字符,当两个或多个字符串都符合这两个条件且长度相等时,会发生什么情况?我如何将最长的变量传递给firstlastvowell方法?好的,下面是步骤-1。)在main方法中声明并初始化最长的变量-
Integer longest=02。)从firstLast元音方法中删除声明行,即删除-
int longest=03。)将类Xjava4的第三行中的
public static void firstlastvonel(String theString)
更改为
public static void firstlastvonel(String theString,Integer longest)
main
方法中调用
firstlastvonel
的行
to
firstlast元音(东西,最长)哈!我喜欢Java 8+解决方案:)