用户输入内容转换成Pig Latin形式。
//(单词的第一个元音字母之前的一道单词后面,以"ay"结尾,英语单词首字母为元音字母或者没有元音字母的以“ay”为后缀。)
package toPigLatin; import java.util.Scanner; public class ToPigLatin { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("enter an line: "); String s = console.nextLine(); while(!s.equals(""))//用户输入空行时,程序自动结束;对象之间的相等不能用“==”来判断! { String[] s0= new String[s.length()]; int m =0; int n =0; int count = 0; while(s.indexOf(" ")!=-1) { n = s.indexOf(" "); s0[count]=s.substring(m,n); count++; s = s.substring(n+1,s.length()); m = 0; } s0[count]=s; for(int i = 0;i <= count;i++) { toPigLatinWord(s0[i]); System.out.print(" "); } System.out.println(); System.out.print("enter an line: "); s = console.nextLine(); } } public static void toPigLatinWord(String word) { int index = -1; String s; for(int i =0;i< word.length();i++) { if(word.charAt(i)=='a'||word.charAt(i)=='e'||word.charAt(i)=='i' ||word.charAt(i)=='o'||word.charAt(i)=='u') { index = i; break; } } if(index == -1 ||index == 0) s = word + "-ay"; else s = word.substring(index,word.length())+ "-" + word.substring(0,index) + "ay"; System.out.print(s); } }
运行结果类似:
enter an line: the blue of bcd
e-thay ue-blay of-ay bcd-ay
enter an line: how much is it
ow-hay uch-may is-ay it-ay
enter an line: