1

I have to design a program that converts English into Pig Latin. I am having difficulty in converting all words contained within the input provided by the user to Pig Latin. With my current code, I achieve an output of:

Input: This is not working
Output: histay is not workingay

While my desired output is:

Input: This is not working.
Output: Histay ishay otnay orkingway

What am I doing wrong? I'm just looking for a push in the right direction.

public class piglatin {

    // A program designed to convert English words in to Pig Latin
  public static void main(String[] args) {

    char a, e, i, o, u, A, E, I, O, U, b;   
    String alphabet = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ";

    Scanner word = new Scanner (System.in);         
    System.out.println("Please enter a word: ");                
    String incoming = word.nextLine();              
    System.out.println("Input: " + incoming);

    String newincoming = incoming.substring(1); 
    String newerincoming = incoming.substring(0,1);

    a = alphabet.charAt(0);
    e = alphabet.charAt(8);
    i = alphabet.charAt(16);
    o = alphabet.charAt(28);
    u = alphabet.charAt(40);
    A = alphabet.charAt(1);
    E = alphabet.charAt(9);
    I = alphabet.charAt(17);
    O = alphabet.charAt(29);
    U = alphabet.charAt(41);
    b = incoming.charAt(0);         

    if ((b == a) || (b == e) || (b == i) || (b == o) || (b == u) || (b == A) || (b == E) || (b == I) || (b == O) || (b == U)) {             
      System.out.println("Output: " + incoming + "hay");
    } else {  
      System.out.println("Output: " + newincoming + newerincoming + "ay");
    }
  }     
}
4

1 回答 1

3

输入:这不工作
输出:他不工作Tay

您正在阅读整行文本并将“pig latin”规则应用于整个句子。这将删除句子的第一个字母并将其放在最后一个单词的末尾。

为了使其工作,您需要split在空格周围输入字符串并将猪拉丁规则应用于循环中的每个单词。我建议创建一个将字符串作为输入并进行猪拉丁语转换的方法。这样,您只需要对该方法返回的数组中的每个单词调用该split方法。

于 2012-10-22T19:09:59.223 回答