2016-10-17 65 views
0

我正在編寫一個程序,它接受一個字符串,將其拆分爲單詞,將單詞轉換爲拉丁語,然後返回結果字符串。我一直在努力。豬拉丁字符串加密

例如,如果我進入這些話不以元音開始到程序中,我得到:

豬 - > igpay
垃圾 - > rashtay
鴨 - > uckday

(對單詞不要從元音開始,它們會刪除第一個字母,並將其與「ay」一起添加到單詞的末尾)

它也適用於單詞以元音開頭(只需將單詞添加到「耶「到最後)。

例如,如果我輸入這些話到程序中,我得到:

吃 - > eatyay
areyay - > areyay
煎蛋 - > omeletyay

現在我遇到的問題是如果我結合了兩個單詞,一個以元音開頭,另一個不出現,則它們都打印出來,就像它們都以元音開頭一樣。

例如,如果我輸入這些話到程序中,我得到:

豬 - > pigyay(應該是igpay)
吃 - > eatyay(正確)

這可能是值得一提的在此程序中需要使用「isVowel」&「pigLatinEncrypt」方法。請忽略程序中的其他方法。

public static void main(String[] args) { 
    // TODO code application logic here 
    String input, message; 
    int ans1, ans2, key; 

    input = JOptionPane.showInputDialog("1. to decrypt a message\n2. to encrypt a message"); 
    ans1 = Integer.parseInt(input); 

    input = JOptionPane.showInputDialog("1. for an entire message reversal encryption\n" 
      + "2. for a Pig-Latin encryption\n3. for a simple Caesar style encryption"); 
    ans2 = Integer.parseInt(input); 

    if (ans2 == 3) { 
     input = JOptionPane.showInputDialog("Please enter a key for encryption"); 
     key = Integer.parseInt(input); 
    } 

    input = JOptionPane.showInputDialog("Please enter the message to encrypt or decrypt"); 
    message = input; 

    if (ans2 == 1) { 
     reverseEncryptandDecrypt(message); 
    } 

    if (ans2 == 2) { 
     String[] words = message.split(" "); 
     if (ans1 == 2) { 
      boolean isVowel = isVowel(words); 
      pigLatinEncrypt(words, isVowel); 
     } 
     if (ans1 == 1) { 
      pigLatinDecrypt(message); 
     } 
    } 

} 

public static void reverseEncryptandDecrypt(String message) { 

    char[] stringToCharArray = message.toCharArray(); 

    System.out.print("You entered the message: "); 

    for (char c : stringToCharArray) { 
     System.out.print(c); 
    } 

    int i = stringToCharArray.length - 1, j = 0; 
    char[] reverse = new char[stringToCharArray.length]; 

    while (i >= 0) { 
     reverse[j] = stringToCharArray[i]; 
     i--; 
     j++; 
    } 

    System.out.print("\n\nThe result is: "); 

    for (char c : reverse) { 
     System.out.print(c); 
    } 
    System.out.println(); 

} 

public static void pigLatinEncrypt(String[] words, boolean isVowel) { 
    for (String word : words) { 
     if (isVowel == true) { 
      System.out.print(word + "yay "); 
     } else { 
      System.out.print(word.substring(1) + word.substring(0, 1) + "ay "); 
     } 

    } 

} 


public static boolean isVowel(String[] words) { 
    boolean isVowel = false; 
    for (String word : words) { 
     if (word.startsWith("a") || word.startsWith("e") || word.startsWith("i") || word.startsWith("o") 
       || word.startsWith("u")) { 
      isVowel = true; 
     } 

    } 
    return isVowel; 
} 

}

回答

1

此方法:

public static void pigLatinEncrypt(String[] words, boolean isVowel) 

需要的話陣列和單個isVowel布爾值。因此,如果有多個單詞,並且其中一些(但不是全部)單詞以元音開頭,則無法告訴該方法。

您需要更改方法定義。它要麼應該只有一個字String(簡單的),或者需要採取的isVowel布爾數組,對應詞的陣列

編輯:當我寫這個,我沒有仔細的看其餘的代碼。但isVowel方法有同樣的問題:它需要一個單詞的數組,但返回一個單個布爾值。基於同樣的原因,這不能起作用 - 如果某些詞以元音開頭,有些則不是,那麼該方法會返回什麼?

如果您可以使isVowel方法採用一個String參數,那將是最簡單的。然後你會多次調用它。如果你想isVowel回報boolean[],該方法將執行類似

boolean[] result = new boolean[words.length]; 

創造boolean陣列具有相同數量的元素words的。

+0

啊,這是有道理的。我只是困惑於如何編寫這個程序。我應該爲我的「isVowel」方法返回值,但我不確定如何將返回的值轉換爲「pigLatinEncrypt」方法的數組。 –

+0

我編輯了我的回覆。 – ajb