为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

长风秋雁 2021-08-25 09:59:07
我正在做一个家庭作业:在字符串中找到一个以辅音开头的唯一元音,并且这个辅音前面有一个元音。示例:“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。你们能帮帮我吗?
查看完整描述

2 回答

?
哈士奇WWW

TA贡献1799条经验 获得超6个赞

使用 aString作为廉价地图来跟踪您已经看过哪些元音。另外,记下您遇到的连续辅音的数量。然后,当你击中一个你以前没有见过的元音前有一个辅音时,你就找到了答案。


public static void main(String[] args)

{

    String s = "eeaaAOEacafu".toLowerCase();


    int consCount = 0;      

    String seenVowels = "";


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

    {

        char c = s.charAt(i);

        if("aeiou".indexOf(c) >= 0)

        {

            if(seenVowels.indexOf(c) == -1)

            {

                if(consCount == 1)

                {

                    System.out.println("Result: " + c);

                    break;

                }

                seenVowels += c;

            }               

            consCount = 0;

        }

        else consCount++;

    }

}

输出:


Result: u

如果我们将 'unique' 理解为我们之前没有见过元音,则上述方法有效。如果元音在输入字符串中必须是唯一的,那么事情就有点复杂了。现在我们必须跟踪满足原始标准的每个元音,但如果我们随后遇到相同元音的另一个实例,则删除解决方案。


下面是一些代码来说明:


public static void main(String[] args)

{   

    String s = "afuxekozue".toLowerCase();


    int consCount = 0;      

    String seenVowels = "";

    String answer = "";


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

    {

        char c = s.charAt(i);

        if("aeiou".indexOf(c) >= 0)

        {

            if(seenVowels.indexOf(c) == -1)

            {

                if(consCount == 1)

                {

                    answer += c;

                }

                seenVowels += c;

            }       

            else if(answer.indexOf(c) >= 0)

            {

                answer = answer.replaceAll(String.valueOf(c), "");;

            }


            consCount = 0;

        }

        else consCount++;

    }


    if(answer.length() > 0)

        System.out.println("Result: " + answer.charAt(0));

}

输出:


Result: o 


查看完整回答
反对 回复 2021-08-25
  • 2 回答
  • 0 关注
  • 144 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信