编写一个程序,将文本翻译成假拉丁文并返回

时间:2017-10-10 17:29:46

标签: java string reverse

编写一个程序,将文本翻译成假拉丁文并返回。将英语翻译成假拉丁语,取每个单词的第一个字母,将其移到单词的末尾,并在每个单词中添加“ay”。 例如,如果您的程序从键盘上读取字符串“The quick brown fox”,那么它应该在屏幕上打印以下文本:“Hetay uickqay rownbay oxfay”。 你也应该实现与此相反的方式,阅读" Iay ikelay rogrammingpay"应该在屏幕上打印"我喜欢编程"。

public class fakelatin {    

  public static void main(String[] args)
  {
     Scanner pig = new Scanner(System.in);
     String word;  original word
     String latin = "";  
     char first;
     boolean cap = false;
     String line;

     System.out.print("enter line to translate: "); //enter the line to translate
     line = pig.nextLine();
    pig = new Scanner(line);

    // loop through all the words in the line
     while (pig.hasNext())  // is there another word?
     {
        word = pig.next();
        first = word.charAt(0);

        if ('A' <= first && first <= 'Z')  // first is capital letter
        {
           first = Character.toLowerCase(first);

           cap = true;
        }
        else
            cap = false;

     // test if first letter is a vowel
        if (first=='a' || first=='e' || first=='i' || first=='o' || first=='u')
           latin = word + "ay";

       else
        {   
           if (cap)
           {
              latin = "" + Character.toUpperCase(word.charAt(1));  // char to String conversion
              latin = latin + word.substring(2) + first + "ay";
           }

           else  
              latin = word.substring(1) + first + "ay";


        }



        System.out.print(latin + " ");

     } 

  }
}

我的输出是iay ikelay rogrammingpay,因为我想要它

import java.util.Scanner;

public class fakelatin2 {  


  public static void main(String[] args)
  {
     Scanner pig = new Scanner(System.in);
     String word; // original word
     String latin = "";  // pig latin translation, init to empty string
     char first,last;
     boolean cap = false;
     String line;

     System.out.print("enter line to translate: ");
     line = pig.nextLine();
     pig = new Scanner(line);

     // loop through all the words in the line
     while (pig.hasNext())  // is there another word?
     {
        word = pig.next();
        first = word.charAt(0);
        last=word.charAt(word.length()-1);

        if ('A' <= first && first <= 'Z')  // first is capital letter
        {
           first = Character.toLowerCase(first);

           cap = true;
        }
        else
            cap = false;




           latin = word.replaceAll("ay", "");
        if(cap)
        {
            latin = "" + Character.toUpperCase(word.charAt(1));
         latin=latin+word.substring(word.length()-1);
        }
        System.out.print(latin + " ");

     } 


  }
}

我得到了一个输出&#34; i ikel rogrammingp&#34;但是不能得到&#34;我喜欢编程&#34;回来

1 个答案:

答案 0 :(得分:0)

要从pig-latin转换回来,请写出算法:

  1. 获取第3封信。那是原来的第一个字母
  2. 检查首字母是否有上限。将步骤#1的结果转换为高位。
  3. 删除最后3个字母。原来的第1 +&#34; ay&#34;
  4. 建立新词。
  5. 像这样:

    while (pig.hasNext())  // is there another word?
    {
        word = pig.next();
    
        // Step 1.
        // Original first letter of word
        last = word.charAt(word.length() - 3);
        // word.length should always be >= 3, but I guess you should check
        // that and print an error if it's not.
    
        // Step 2.
        // First char in latin word
        first = word.charAt(0);
        if (Character.isUpperCase(first))  // first is capital letter
        {
            last = Character.toUpperCase(last);
        }
    
        // Steps 3. and 4.
        String original = last + word.substring(0, word.length() - 3).toLowerCase();
    
        System.out.print(original + " ");
    }
    

    注意:我不知道如何转换以元音开头的单词。例如:&#34; end&#34;是&#34; enday&#34;在假拉丁语中。当你转回来时,你怎么知道它&#34;结束&#34;或&#34; den&#34;? (因为&#34; den&#34;在假拉丁语中也是&#34; enday&#34;)。但是,作业并没有说明以元音开头的单词,因此您应该从转换器中删除它。问题解决了。