Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在piglatin.java上更改句子中的所有单词_Java - Fatal编程技术网

如何在piglatin.java上更改句子中的所有单词

如何在piglatin.java上更改句子中的所有单词,java,Java,我正在写一个叫做piglatin的程序。程序将一直运行,直到用户类型退出。任何以元音开头的单词,都必须在单词的末尾加上“way”。如果单词以辅音开头,那么我必须将辅音移到单词的末尾,并添加单词“ay”。我唯一的问题是如何使我的程序能够单独更改所有单词,而不是基于句子的第一个单词。例如,“他很好”应该变成“ehay isway icenay”,但我的输出是这样的“e是尼斯海”。非常感谢你的帮助,我真的很感激。这是我的密码 import java.util.Scanner; public clas

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

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

}

可以将输入拆分为字符串数组,然后循环

String[] words = yourSentence.split(" ");
for(int i = 0; i < words.length; i++){
   //Do stuff
}
String[]words=yourscreation.split(“”);
for(int i=0;i
你很接近。我将
你的句子
拆分到数组
标记
中。然后我初始化了
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"));
    }
这也解决了多个字母/单音词,例如以“ch”、“sh”和“th”开头的单词。如果你想正确处理标点符号,你将有更多的工作要做


我还在每一行输入后都添加了一行新词,以整理输出的表示。

在应用Pig拉丁语规则之前,您需要先将句子拆分为单词。您应该查看字符串#split。您还可以对(字符串单词:words)执行
操作,操作要短得多。如果使用标准for循环,您可以修改数组,然后在末尾执行
StringUtils.join(单词“”
。这可能会让事情变得更简单。
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"));
    }
}