我正在做一个家庭作业:
在字符串中找到一个以辅音开头的唯一元音,并且这个辅音前面有一个元音。
示例:“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。
你们能帮帮我吗?
哈士奇WWW
相关分类