如何改变piglatin.java句子中的所有单词

时间:2014-11-30 23:34:49

标签: java

我正在编写一个名为piglatin的程序。程序将一直运行,直到用户输入退出。任何单词都以元音开头,单词的结尾必须添加“方式”。如果单词以辅音开头,那么我必须将辅音移到单词的末尾并添加单词“ay”。我唯一的问题是如何使我的程序能够单独更改所有单词而不是基于句子的第一个单词。例如“他很好”应该变成“ehay isway icenay”但我的输出就像这样“e is nicehay”。非常感谢你的帮助,我真的很感激。这是我的代码

import java.util.Scanner;

public class PigLatin
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner( System.in );
        String yourSentence="";
        String[] tokens;

        do
        {
            System.out.print("Enter your words here: ");
            yourSentence = input.nextLine();

            if( yourSentence.startsWith("a") || yourSentence.startsWith("e") || yourSentence.startsWith("i") ||
                yourSentence.startsWith("o") || yourSentence.startsWith("u"))
            {
                System.out.print(yourSentence+ "way");
            }
            else
            {
                System.out.print(yourSentence.substring(1)+yourSentence.substring(0,1)+"ay");
            }   
        }

        while(!yourSentence.equals("quit"));
    }

}

3 个答案:

答案 0 :(得分:0)

您可以将输入拆分为String数组,然后循环。

String[] words = yourSentence.split(" ");
for(int i = 0; i < words.length; i++){
   //Do stuff
}

答案 1 :(得分:0)

你很亲密。我在数组yourSentence中拆分了token。然后我初始化newSentence并使用for循环遍历所有标记。每个项目都添加到newSentence,最后打印出这个新句子。

public static void main(String[] args)
    {
        Scanner input = new Scanner( System.in );
        String yourSentence="";
        String[] tokens;

        do
        {
            System.out.println("Enter your words here: ");
            yourSentence = input.nextLine();
            tokens = yourSentence.split(" ");

            String newSentence = "";

            for(String token : tokens) {
                if( token.startsWith("a") || token.startsWith("e") || token.startsWith("i") ||
                        token.startsWith("o") || token.startsWith("u"))
                {
                    newSentence += token + "way ";
                }
                    else
                {
                    newSentence += token.substring(1) + token.substring(0,1) + "ay ";
                }   
            }

            System.out.println(newSentence);

        }

        while(!yourSentence.equals("quit"));
    }

答案 2 :(得分:0)

import java.util.Scanner;

public class PigLatin {
    public static void main(String[] args) {
        Scanner input = new Scanner( System.in );
        String yourSentence="";
        do {
            String[] words;
            System.out.print("Enter your words here: ");
            yourSentence = input.nextLine();
            words = yourSentence.split(" ");
            for (String word : words) {
                if (word.startsWith("a") || word.startsWith("e") || word.startsWith("i") || word.startsWith("o") || word.startsWith("u"))
                    System.out.print(word + "way ");
                else if (word.startsWith("sh") || word.startsWith("ch") || word.startsWith("th"))
                    System.out.print(word.substring(2)+word.substring(0,2)+"ay ");
                else
                    System.out.print(word.substring(1)+word.substring(0,1)+"ay ");
        }
        System.out.println();
        } while(!yourSentence.equals("quit"));
    }
}

这也解决了多个字母/单个声音的单词,例如那些以&#34; ch&#34;,&#34; sh&#34;和&#34; th&#34;开头的单词。如果您希望能够正确处理标点符号,那么您还有更多工作要做。

我还在每行输入后添加了一个新行,以整理输出的演示文稿。