2017-10-10 70 views
0

編寫一個將文本翻譯爲假拉丁文並返回的程序。將英語翻譯爲假拉丁文,方法是將每個單詞的第一個字母移動到單詞的結尾,併爲每個單詞添加「ay」。 作爲一個例子,如果你的程序從鍵盤上讀取字符串「快速棕色狐狸」,那麼它應該在屏幕上打印下面的文本:「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 + " "); 

    } 


    } 
} 

爲「我ikel rogrammingp」,但沒能得到我得到的輸出「我喜歡編程」回到

+0

'word.replaceAll(「ay」,「」)'似乎很危險,如果你的單詞是play(laypay)?它會變形,你可能想使用'word.replaceAll(「ay $」,「」)'(使用[regex](https://docs.oracle.com/javase/8/docs/api/java) /util/regex/Pattern.html#sum))或'word.substring(0,word.length() - 2)' – phflack

+0

它給出的輸出如「I Iikel Iikelrogrammingp」 – arfa

+0

您刪除「ay」的所有實例,但永遠不要將最後一個字符移動到前面,你可能想'latin = latin.substring(latin.length() - 1)+ latin.substring(0,latin.length() - 1);' – phflack

回答

0

要轉換從豬拉丁回,寫出來的算法:

  1. 獲得第三,從-最後一個字母。這是原始的第一個字母
  2. 檢查是否第一個字母是上限。將步驟#1的結果轉換爲上一個。
  3. 刪除最後3個字母。原來的第一個+「唉」
  4. 建立新單詞。

像這樣:

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 + " "); 
} 

注:我不明白你怎麼可以轉換回以元音開頭的單詞。例如:「end」是假拉丁語中的「enday」。當你轉回來時,你怎麼知道它是「結束」還是「登錄」? (因爲拉丁文中的「den」也是「enday」)。然而,作業並沒有提到任何關於以元音開頭的單詞,所以你應該從你的轉換器中刪除它。問題解決了。

+0

您應該保持一致,使用'Character.isUpperCase第一)'如果你使用'Character.toUpperCase(last)' – phflack

+0

@phflack好的。更新。 –

相關問題