尝试使用带有方法的子字符串作为参数,但不断收到错误[重复]

问题描述 投票:-1回答:1

这个问题在这里已有答案:

我一直在尝试使用子字符串返回一行代码,但每次我尝试编译时都会收到错误“找不到符号 - 方法findFirstVowel()”它出现在最后一行代码中。为什么这不起作用? findFirstVowel应该返回一个整数吧?并且此方法也不需要输入 - 因此参数应为(0,findFirstVowel的值)。有谁知道如何解决这一问题?谢谢!

public class words
{
    private String w;

    /**
     * Default Constructor for objects of class words
     */
    public words()
    {
        // initialise instance variables
        w="";
    }

    /**
     * Assignment constructor
     */
    public words(String assignment)
    {
        w=assignment;
    }

    /**
     * Copy constructor
     */
    public words(words two)
    {
        w=two.w;
    }

    /**
     * Pre: 0<=i<length( )
     * returns true if the character at location i is a vowel (‘a’, ‘e’, ‘i', ‘o’, ‘u’ only), false if not
     */
    public boolean isVowel(int i)
    {
        char vowel = w.charAt(i);
        return vowel == 'a' || vowel == 'e' || vowel == 'i' || vowel == 'o' || vowel == 'u';
    }

    /**
     * determines whether the first vowel in the String is at location 0, 1, 2, or 3 (don’t worry about exceptions)
     */
    public int findFirstVowel()
    {
        if (isVowel(0))
            return 0;
        else if (isVowel(1))
            return 1;
        else if (isVowel(2))
            return 2;
        else if (isVowel(3))
            return 3;
        else
            return -1;
    }

    /**
     * returns the Pig-Latin version of the String
     */
    public String pigify()
    {
        return w.substring(w.findFirstVowel()) + w.substring(0,w.findFirstVowel()) + "ay";

    }
java
1个回答
1
投票

findFirstVowel不是类String的一部分,它是你班级的一部分。所以不要在w上调用它,它是类String的一个实例

这是固定代码

public String pigify() {
    return w.substring(findFirstVowel()) + w.substring(0, findFirstVowel()) + "ay";
}
© www.soinside.com 2019 - 2024. All rights reserved.