gpt4 book ai didi

java - 写入输出java时从输入中删除相同的字母

转载 作者:行者123 更新时间:2023-12-02 06:58:51 26 4
gpt4 key购买 nike

我需要一些帮助来解决以下代码中遇到的问题。

package piglatin;
public class asdg {

public static void main(String[] args) {

String word = "fifteen";
int vowelSpot = findFirstVowelPosition(word);
String wordBeg = "";
String wordEnd = "ay";
String wordNew = "";

System.out.println(vowelSpot);

wordBeg = word.substring(0,vowelSpot-1);

System.out.println(wordBeg);

word = word.replace(wordBeg,"");

System.out.println(word);

wordNew = word + wordBeg + wordEnd;

System.out.println(wordNew);
}


public static int findFirstVowelPosition(String word)
{
int vowelPosition = -1;
word = word.trim();

for(int i=1; i <=word.length(); i++)
{
if ((word.charAt(i-1) == 'a') ||(word.charAt(i-1) == 'A')
||(word.charAt(i-1) == 'e') ||(word.charAt(i-1) == 'E')
||(word.charAt(i-1) == 'i') ||(word.charAt(i-1) == 'I')
||(word.charAt(i-1) == 'o') ||(word.charAt(i-1) == 'O')
||(word.charAt(i-1) == 'u') ||(word.charAt(i-1) == 'U')
||(word.charAt(i-1) == 'y') ||(word.charAt(i-1) == 'Y')){
vowelPosition = i;
return vowelPosition;
}
}
return vowelPosition;
}
}

此代码的目的是获取字符串 Word 并将其放入 Piglatin 中。单词将以辅音开头。我遇到的问题是,当字符串包含特定辅音,然后是第一个元音和相同的辅音时,它将删除第二个辅音。

此特定代码不适用于家庭作业,但我会将其应用于家庭作业。

最佳答案

我明白了:

替换这一行:

word = word.replace(wordBeg,"");

word = word.replaceFirst(wordBeg,"");

查看相关文档:http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

关于java - 写入输出java时从输入中删除相同的字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16945028/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com