如何将用户输入字符串转换为pig latin?

时间:2015-09-02 21:55:34

标签: java if-statement for-loop while-loop

我试图将用辅音开头的用户输入的字符串转换为猪拉丁语,将所有辅音移动到单词的结尾,直到单词以元音开头,然后在单词的末尾添加“ay” 。我有一个for循环应该这样做,但由于某种原因,它什么也没输出。我在这做错了什么?我很难过。

以下是代码:

import java.util.Scanner;

public class two {
    public static void main(String[] args) {

        System.out.println("Please enter a word");
        Scanner word = new Scanner(System.in);
        String pigLatin = word.nextLine();
        while (!pigLatin.equalsIgnoreCase("quit")) {
            if (isVowel(pigLatin.charAt(0))) {
                pigLatin = (pigLatin + "way");
                System.out.println(pigLatin);
            } 
            else {
                for (int i = 0; i < pigLatin.length(); i++) {
                    char firstChar = pigLatin.charAt(0); 
                    pigLatin = pigLatin.substring(1);
                    pigLatin = pigLatin + firstChar;
                    if (i >= pigLatin.length())
                    {
                        pigLatin = pigLatin + "ay";
                        System.out.println(pigLatin);
                    }
                }

            }

            System.out.println("Please enter a word");
            pigLatin = word.nextLine();
        }
        word.close();

    }

    private static boolean isVowel(char ch) {
        char v = Character.toLowerCase(ch);
        if (v == 'a' || v == 'e' || v == 'i' || v == 'o' || v == 'u') {
            return true;
        }

        else {
            return false;
        }

    }

}

2 个答案:

答案 0 :(得分:1)

你需要一个小于或等于&lt; = i,否则我永远不会大于或等于pigLatin.length()。

                for (int i = 0; i <= pigLatin.length(); i++) {
                    char firstChar = pigLatin.charAt(0); 
                    pigLatin = pigLatin.substring(1);
                    pigLatin = pigLatin + firstChar;
                    if (i >= pigLatin.length())
                    {
                        System.out.println(pigLatin);
                    }
                }

答案 1 :(得分:0)

你的for循环条件是

i < pigLatin.length()

然后你说如果

if(i >= pigLatin.length()){....}

因此这种情况永远不会成立,因此没有输出,

见下面的代码,

import java.util.Scanner;

/**
 *
 * @author rahmat waisi
 */
public class PigLatin {

    public static void main(String[] args) {
        try (Scanner scanner = new Scanner(System.in)) {
            while (true) {
                // System.out.print("Please enter a word: , Enter [ quit ] for exit : ");
                String pigLatin = scanner.nextLine();
                if (pigLatin.equals("quit")) {
                    break;
                }
                if (isVowel(pigLatin.charAt(0))) {
                    pigLatin += "ay";
                    System.out.println(pigLatin);
                } else {
                    String output = "";
                    int separation_index = findFirstVowel(pigLatin);
                    if (separation_index ==-1) {
                        System.out.println(pigLatin+"ay");
                        continue;
                    }
                    output+= pigLatin.substring(separation_index);
                    output+= pigLatin.substring(0, separation_index) + "ay";
                    System.out.println(output);
                }
            }
        }
    }

    private static boolean isVowel(char ch) {
        char v = Character.toLowerCase(ch);
        return v == 'a' || v == 'e' || v == 'i' || v == 'o' || v == 'u';
    }

    private static int findFirstVowel(String str) {
        for (int i = 0; i < str.length(); i++) {
            if (isVowel(str.charAt(i))) {
                return i;
            }
        }
        return -1;
    }

}

这里有一些输入:

pig
banana
trash
happy
duck
glove
eat
omelet
are
ffff
quit

他们的输出是:

igpay
ananabay
ashtray
appyhay
uckday
oveglay
eatay
omeletay
areay
ffffay