-3
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)*/
4

1 回答 1

1

永远相信错误。他们知道他们在说什么。如果他们说你超出范围 -你确实超出了范围,你甚至知道确切的位置。您没有标记这条线,但不难发现:

if (wordsArray[j].charAt(i)=='a' || ...

什么可能超出范围?要么wordsArray[j]不存在,要么不存在charAt(i)。它是哪一个?

j受 wordsArray 中的单词数量限制。我们在这里很清楚。这就留下了另一个。现在,是什么i意思...

for ( int i = 1; i<= wordsArray.length; i++)

它似乎也受 wordsArray 中的单词数量的限制。从 1 开始,因此它永远不会显示第一个单词,但这无关紧要,因为获取单词不是您使用它的目的。实际上,导致错误的原因是您希望第 j 个单词的字母与输入中的单词一样多

于 2015-10-15T23:11:43.653 回答