//我需要帮助我的其他if语句.如果单词以元音开头,则添加到单词末尾的方式.如果这个单词以辅音开头,那么你可以将辅音放在最后并添加一个.
我的问题是,如果我的第一个字母带有元音一个字,它就像有一个辅音一样贯穿其中.如果我输入“是”,我会得到“arewayreaay”而不是“areway”.
public class piglatin {
public static void main(String[] args) {
String str = IO. readString();
String answer = "";
if(str.startsWith("a"))
System.out.print(str + "way");
if(str.startsWith("e"))
System.out.print(str + "way");
if(str.startsWith("i"))
System.out.print(str + "way");
if(str.startsWith("o"))
System.out.print(str + "way");
if(str.startsWith("u"))
System.out.print(str + "way");
else{
char i = str.charAt(0);
answer = str.substring( 1, str.length());
System.out.print(answer + i + "ay");
}
}
}
解决方法:
if(str.startsWith("a") || str.startsWith("e") || str.startsWith("i") ... (and so on)) {
System.out.print(str + "way");
} else{
char i = str.charAt(0);
answer = str.substring( 1, str.length());
System.out.print(answer + i + "ay");
}
或者if / else if / else
说明:
// combine all the if blocks together .. if you dont it checks for vowel 'a' and prints
// 'areay' then it checks for vowel 'u' and enters the else block .. where it again
// operates on 'are' (check immutability of strings) gets charAt(0) i.e 'a' and prints
// 'rea' and since you concatenated 'ay' ... the final output = 'arewayreaay'
标签:java,io,apache-pig,string
来源: https://codeday.me/bug/20190718/1494559.html