import java.util.Scanner;
import java.util.Arrays;
public class PigLatin
{
public static void main (String[] args) {
System.out.println("Translate or Reverse");
Scanner choose = new Scanner(System.in);
String choice = choose.nextLine();
Scanner input = new Scanner(System.in);
String ipWord = input.nextLine();
if ( choice.equals("Translate"))
{
System.out.println(translator(ipWord));
}
else if( choice.equals("Reverse"))
{
System.out.println(reverse(ipWord));
}
else
{
System.out.println("Error");
}
}
public static String translator(String ipWord)
{
String[] wordsArray = ipWord.split(" ");
String result = "";
for(int j =0;j<wordsArray.length;j++)
{
if(wordsArray[j].charAt(0)=='a' || wordsArray[j].charAt(0) =='e' || wordsArray[j].charAt(0)== 'i' || wordsArray[j].charAt(0)== 'o' || wordsArray[j].charAt(0)== 'u'|| wordsArray[j].charAt(0)=='A' || wordsArray[j].charAt(0) =='E' || wordsArray[j].charAt(0)== 'I' || wordsArray[j].charAt(0)== 'O' || wordsArray[j].charAt(0)== 'U')
/* I check if it is begin with vowel. If it begin with vowels + "yay". For example "Eat">"Eatyay"*/
{
result = result+ " " + wordsArray[j] +"yay";
}
else
{
for ( int i = 1; i<= wordsArray.length; i++)
{
if(wordsArray[j].charAt(0)=='B'||wordsArray[j].charAt(0)=='C'||wordsArray[j].charAt(0)=='D'||wordsArray[j].charAt(0)=='F'||wordsArray[j].charAt(0)=='G'||wordsArray[j].charAt(0)=='H'||wordsArray[j].charAt(0)=='J'||wordsArray[j].charAt(0)=='K'||wordsArray[j].charAt(0)=='L'||wordsArray[j].charAt(0)=='M'||wordsArray[j].charAt(0)=='N'||wordsArray[j].charAt(0)=='P'||wordsArray[j].charAt(0)=='Q'||wordsArray[j].charAt(0)=='R'||wordsArray[j].charAt(0)=='S'||wordsArray[j].charAt(0)=='T'||wordsArray[j].charAt(0)=='V'||wordsArray[j].charAt(0)=='X'||wordsArray[j].charAt(0)=='Z'||wordsArray[j].charAt(0)=='W'||wordsArray[j].charAt(0)=='Y')
/* I check if it begin with a UpperCase Consonants, then I will take every consonant before the first vowel, move it to the back and add "ay"
. For example, "World" > "Orldway" ; "String" > "Ingstryay" */
{
if (wordsArray[j].charAt(i)=='a' || wordsArray[j].charAt(i) =='e' || wordsArray[j].charAt(i)== 'i' || wordsArray[j].charAt(i)== 'o' || wordsArray[j].charAt(i)== 'u')
{
result = result+ " " + wordsArray[j].substring(i,i+1).toUpperCase( )+ wordsArray[j].substring(i+1)+wordsArray[j].substring(i-1,i).toLowerCase( ) +"ay";
}
}
else
{
if (wordsArray[j].charAt(i)=='a' || wordsArray[j].charAt(i) =='e' || wordsArray[j].charAt(i)== 'i' || wordsArray[j].charAt(i)== 'o' || wordsArray[j].charAt(i)== 'u'|| wordsArray[j].charAt(j)=='A' || wordsArray[j].charAt(j) =='E' || wordsArray[j].charAt(j)== 'I' || wordsArray[j].charAt(j)== 'O' || wordsArray[j].charAt(j)== 'U')
{
result = result+ " " + wordsArray[j].substring(i)+ wordsArray[j].substring(0,i) +"ay";
}
}
}
}
}
return result;
}
public static String reverse ( String ipWord)
{
String[] wordsArray = ipWord.split(" ");
String result = "";
for( int h = 0 ; h < wordsArray.length ; h++)
{
if(wordsArray[h].charAt(wordsArray[h].length( )-3)=='y')
{
result = result + " " + wordsArray[h].charAt(wordsArray[h].length()-4) + wordsArray[h].substring(0,wordsArray[h].length()-4);
}
else
{
result = result + wordsArray[h].substring(0,wordsArray[h].length( )-2);
}
}
return result;
}
}
这是我修复的。我认为它不能翻译超过 2 个单词。如果我尝试翻译嗨,你好吗。错误是:
java.lang.StringIndexOutOfBoundsException: String index out of range: 2
at java.lang.String.charAt(Unknown Source)
at PigLatin.translator(PigLatin.java:56)
at PigLatin.main(PigLatin.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)*/