猿问

这个元音问题有什么问题?超出索引

我正在做一个家庭作业:


在字符串中找到一个以辅音开头的唯一元音,并且这个辅音前面有一个元音。

示例:“eeaaAOEacafu” 结果是: u


我已经做了什么:


主类


public class Principal {

    public static void main(String[] args) {

        // TODO Auto-generated method stub


        Stream str = new Stream();


        str.setText("eeaaAOEacafu");

        System.out.println(str.returnChar(str.getVowel()));

    }

流类


public class Stream {


    String text;

    char vowel;


public String getText() {

    return texto;

}


public void setText(String text) {

    this.text = text;

}


public char getVowel() {

    return vowel;

}


public void setVowel(char vowel) {

    this.vowel = vowel;

}



    public boolean isVowel(String str) {

    str = str.toLowerCase();

    for(int i=0; i<str.length(); i++) {

        char c = str.charAt(i);

        if(c=='a' || c=='e' || c=='i' || c=='o'|| c=='u') {

            return true;

        } else {

            return false;

        }

    }

    return false;

}



public char returnChar(String str) {

    char last;

    char next;

    char result = '0';

    int j=1;

    for(int i=0; i<str.length(); i++) {

        last = str.charAt(i-1);

        next = str.charAt(i+1);

        j++;

        if(!vogal(str.charAt(i))) {

            if(vogal(last) && vogal(next)) {

                result = next;

            }

        }

    }

    this.setVowel(result);

    return result;

} }

这将返回:字符串索引超出范围:-1


这个 j=1,是为了修复这个 -1 超出范围。它修复了,但我得到了新的:由于下一个,超出了范围 11。


问题是:我必须使用纯 Java 而没有 API。


你们能帮帮我吗?


长风秋雁
浏览 145回答 2
2回答

哈士奇WWW

使用 aString作为廉价地图来跟踪您已经看过哪些元音。另外,记下您遇到的连续辅音的数量。然后,当你击中一个你以前没有见过的元音前有一个辅音时,你就找到了答案。public static void main(String[] args){&nbsp; &nbsp; String s = "eeaaAOEacafu".toLowerCase();&nbsp; &nbsp; int consCount = 0;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; String seenVowels = "";&nbsp; &nbsp; for(int i=0; i<s.length(); i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; char c = s.charAt(i);&nbsp; &nbsp; &nbsp; &nbsp; if("aeiou".indexOf(c) >= 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(seenVowels.indexOf(c) == -1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(consCount == 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Result: " + c);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; seenVowels += c;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; consCount = 0;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else consCount++;&nbsp; &nbsp; }}输出:Result: u如果我们将 'unique' 理解为我们之前没有见过元音,则上述方法有效。如果元音在输入字符串中必须是唯一的,那么事情就有点复杂了。现在我们必须跟踪满足原始标准的每个元音,但如果我们随后遇到相同元音的另一个实例,则删除解决方案。下面是一些代码来说明:public static void main(String[] args){&nbsp; &nbsp;&nbsp; &nbsp; String s = "afuxekozue".toLowerCase();&nbsp; &nbsp; int consCount = 0;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; String seenVowels = "";&nbsp; &nbsp; String answer = "";&nbsp; &nbsp; for(int i=0; i<s.length(); i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; char c = s.charAt(i);&nbsp; &nbsp; &nbsp; &nbsp; if("aeiou".indexOf(c) >= 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(seenVowels.indexOf(c) == -1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(consCount == 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; answer += c;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; seenVowels += c;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if(answer.indexOf(c) >= 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; answer = answer.replaceAll(String.valueOf(c), "");;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; consCount = 0;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else consCount++;&nbsp; &nbsp; }&nbsp; &nbsp; if(answer.length() > 0)&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Result: " + answer.charAt(0));}输出:Result: o&nbsp;
随时随地看视频慕课网APP

相关分类

Java
我要回答