如何将用户给定的String转换为Pig Latin?

时间:2015-06-23 20:57:19

标签: java string loops

我试图将用户的字符串转换为Pig Latin。我不能使用任何特殊的类,方法或数组。除了任何类型的循环之外,我只能使用扫描程序创建一个对象来从用户和.length和.charAt中获取字符串。 (也不能使用switch语句或break关键字)

以下是我的输出结果的示例:

Enter a line of text: this is a test.

Input : this is a line of text.
Output: his-tay is-way a-way ine-lay of-way ext-tay.

这是我的代码,我只能让我的代码使用一个单词,并且最后必须有一个空格。根据循环,一次只能运行一个循环。我不知道如果我得到一个完整的字符串该怎么办。

我知道当用户输入一个发出新单词信号的空间时,当他们进入一个句号时,就会发出结束信号。

2 个答案:

答案 0 :(得分:0)

我很难理解你的代码。 (看起来你一次尝试两种方式吗?)无论如何,我相信我能够理解你的问题。这是一个可编辑且可运行的示例:

import java.util.Scanner;

public class PigLatin
{
   public static void main(String[] args)
   {
      System.out.print("Enter a line of text: ");
      Scanner keyboard = new Scanner(System.in);
      String text = keyboard.nextLine();

      System.out.println("\nInput: " + text);
      System.out.print("Output: ");

      if (text != null && text.length() > 0)
      {
         int i = 0;
         // this iterates through the whole string, stopping at a period or
         // the end of the string, whichever is closer
         while (i < text.length() && text.charAt(i) != '.')
         {
            // these three variables only exist in this code block,
            // so they will be re-initialized to these values
            // each time this while loop is executed
            char first = '\0'; // don't worry about this, I just use this value as a default initializer
            boolean isFirst = true;
            boolean firstIsVowel = false;
            // each iteration of this while loop should be a word, since it 
            // stops iterating when a space is encountered
            while (i < text.length()
                   && text.charAt(i) != ' '
                   && text.charAt(i) != '.')
            {
               // this is the first letter in this word
               if (isFirst)
               {
                  first = text.charAt(i);
                  // deal with words starting in vowels
                  if (first == 'a' || first == 'A' || first == 'e' || first == 'E'
                      || first == 'i' || first == 'I' || first == 'o' || first == 'O'
                      || first == 'u' || first == 'U')
                  {
                     System.out.print(first);
                     firstIsVowel = true;
                  }
                  // make sure we don't read another character as the first 
                  // character in this word
                  isFirst = false;
               }
               else
               {
                  System.out.print(text.charAt(i));
               }

               i++;
            }

            if (firstIsVowel)
            {
               System.out.print("-tay ");
            }
            else if (first != '\0')
            {
               System.out.print("-" + first + "ay ");
            }

            i++;
         }

         System.out.print('\n'); //for clean otuput
      }
   }
}

在那里有一些评论可能有助于指导您完成我的逻辑。这几乎绝对不是最有效的方法(即使有你的限制),因为我只是把它作为你可以使用的逻辑类型的一个例子。

答案 1 :(得分:0)

您可以将其分解为单词,然后在您点击空格或句点时处理当前单词:

System.out.print("Enter a line of text: ");
Scanner keyboard = new Scanner(System.in);
String text = keyboard.nextLine();

System.out.println("\nInput: " + text);
System.out.print("Output: ");

String curWord = "";

for (int i = 0; i < text.length(); i++) {
    if (text.charAt(i) == ' ' || text.charAt(i) == '.') {
        if (curWord.charAt(0) == 'a' || curWord.charAt(0) == 'e' ||
            curWord.charAt(0) == 'i' || curWord.charAt(0) == 'o' ||
            curWord.charAt(0) == 'u') {
            System.out.print(curWord + "-way ");
        } else {
            for (int j = 1; j < curWord.length(); j++) {
                System.out.print(curWord.charAt(j);
            }
            System.out.print("-" + curWord.charAt(0) + "ay ");
            //System.out.print(curWord.substring(1)+"-"+curWord.charAt(0)+"ay ");
        }
        curWord = "";
    } else {
        curWord += text.charAt(i);
    }
}