2013-08-07 48 views
1

我對Java非常陌生,正在嘗試創建一個程序來將句子翻譯成Pig Latin,將該單詞的第一個字母移到末尾並在末尾添加「y」if第一個字母是元音,最後是「ay」。我需要爲此使用隊列。目前我的程序正在終止,我想知道是否有人能夠發現我要去哪裏或哪裏接下去。謝謝!Java豬拉丁語句子翻譯器使用隊列

import MyQueue.QueueList; import java.util.Scanner;

公共類PigLatin {

public static void main (String[] args) 
{ 


    Scanner scan = new Scanner (System.in); 

    QueueList word = new QueueList(); 

    String message; 
    int index = 0; 
    char firstch; 
    System.out.print ("Enter an English sentence: "); 
    message = scan.nextLine(); 
    System.out.println ("The equivalent Pig Latin sentence is: "); 

    firstch = Character.toLowerCase(message.charAt(0)); 


      if (firstch != 'a' && firstch != 'e' && firstch != 'i' && firstch != 'o' && firstch != 'u' 
        && firstch != ' ') 
       { 
        for (index = 1; index < message.length(); index++) 

         { 
          word.enqueue(new Character(message.charAt(index))); 
         } 

        word.enqueue(new Character (firstch)); 
        word.enqueue(new Character ('a')); 
        word.enqueue(new Character ('y')); 
        word.enqueue(new Character(' ')); 

       } 

      else if (firstch == 'a' || firstch == 'e' || firstch == 'i' || firstch == 'o' || firstch == 'u') 
      { 
       while (message.charAt(index) != ' ') 
       { 
       for (index = 1; index < message.length(); index++) 
         { 
          word.enqueue(new Character(message.charAt(index))); 
         } 
       } 
       word.enqueue((firstch)); 
       word.enqueue(('y')); 
       word.enqueue((' ')); 
      } 
      else if (message.charAt(index) == ' ') 
      { 
       index++; 
       firstch = message.charAt(index); 
      } 



    while (!word.empty()) 
     System.out.print(word.dequeue()); 

} 

}

這裏是從myQueue中包QueueList類:

// QueueList.java 
// 
// Class QueueList definition with composed List object.  

package MyQueue; 

public class QueueList { 

    private List a_queue; 

    public QueueList() { 
     a_queue = new List("queue"); 
    } 

    public Object peek() throws EmptyListException { 
     if (a_queue.isEmpty()) 
      return null; 
     else 
      return a_queue.getFirstObject(); 
    } 

    public void print() { 
     a_queue.print(); 
    } 

    public void enqueue(Object object) { 
     a_queue.insertAtBack(object); 
    } 

    public Object dequeue() throws EmptyListException { 
     return a_queue.removeFromFront(); 
    } 

    public boolean empty() { 
     return a_queue.isEmpty(); 
    } 

} 
+2

請問您可以在您的代碼上運行格式化程序嗎?跟蹤你的條件有點困難。 – chrylis

+0

縮進修復。 ctrl-k縮進不能很好地與製表符配合使用,因此可能會造成第一次編輯的錯誤。 –

+0

謝謝你的縮進幫助。我已將其編輯到我的程序的最新版本,該程序目前不會翻譯,而是完全打印輸入的內容。 – user2659030

回答

2

您還沒有進入你的第二個前重置指數爲0 while循環。由於index == message.length()在第一個循環結束後,第二個循環立即終止。

編輯:回覆:您的最新更新。

在您的第二個循環中,您只能從字隊列中取出第一個message.length()字符。如果你已經添加 - 到最後你不會看到它。相反,循環隊列的長度,而不是輸入消息的長度:

while (!word.empty()) 
    System.out.print(word.dequeue()); 

我可以當場用你的邏輯許多其他問題(你是不是刪除的第一個字母,你是不是在處理個別單詞這句話),但是上面的改變應該足以讓你打印隊列中的內容,並在調試的過程中發送給你。

+0

我修復了這個問題(非常感謝你收到它)並上傳了一個修改後的版本,但是我的程序只打印了我輸入的句子,並在句尾加上了第一個字母。有什麼建議麼? – user2659030

+0

謝謝,傑森。我不完全確定如何處理單個單詞,但會查看它。 – user2659030

+0

我有一個關於我的for循環的問題。那些不正確,因爲我不應該設置索引= 1來處理單個單詞? – user2659030