【问题标题】:How to turn a user given String into Pig Latin?如何将给定字符串的用户变成 Pig Latin?
【发布时间】:2015-06-25 01:27:05
【问题描述】:

我正在尝试将来自用户的字符串转换为 Pig Latin。我不能使用任何特殊的类、方法或数组。除了任何类型的循环之外,我只能使用 Scanner 创建一个对象以从用户那里获取字符串以及 .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.

这是我的代码,我的代码只能使用一个单词,并且它的末尾必须有一个空格。取决于循环,一次只能工作一个循环。如果我得到一个完整的字符串,我不确定该怎么办。

我知道当用户输入一个表示新单词的空格时,当他们输入一个句点时,表示结束。

【问题讨论】:

  • 对我来说似乎是一个很好的家庭作业问题 :)

标签: java string loops


【解决方案1】:

我很难理解您的代码。 (看起来您正在尝试同时以两种方式进行操作?)无论如何,我相信我能够理解您的问题。这是一个可编译且可运行的示例:

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
      }
   }
}

其中有一些 cmets 可能会帮助您了解我的逻辑。这几乎绝对不是最有效的方法(即使有您的限制),因为我只是将其作为您可以使用的逻辑类型的示例。

【讨论】:

  • 我试过了,效果很好!这真的很有意义。但是我在程序编译时有一个问题,为什么它运行但最后还是有错误?我收到 Exception in thread main 错误,但它仍然输出答案
  • 啊,是的,那是我的错误。现在已修复。我可能使用了也可能没有使用仅使用字符串This is a test. 进行测试的非常彻底的测试过程......看起来最后没有句点的字符串失败了。无论如何,异常是因为 i &lt; text.length() 最初在内部 while 循环中的 test.charAt(i) != ' ' 之后引起的。按照它们现在的顺序,它将起作用,因为一旦 i &lt; text.length() 失败,while 语句就会失败,这意味着永远不会调用 text.charAt(i)(这是导致异常的方法调用)。
  • 啊,是的,这是有道理的,幸运的是,假设用户在末尾加上句号是安全的,但这样做更加彻底,非常感谢。我现在要自己再试一次
【解决方案2】:

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

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

【讨论】:

  • 我不能使用 .substring 但这是我的第一个想法,谢谢
  • 你不必在那里使用子字符串。查看更新的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-07
  • 1970-01-01
  • 2017-08-09
  • 2014-05-10
  • 1970-01-01
  • 1970-01-01
  • 2018-04-06
相关资源
最近更新 更多