Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.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
Java如何将字符串翻译成拉丁语?_Java_String_Character - Fatal编程技术网

Java如何将字符串翻译成拉丁语?

Java如何将字符串翻译成拉丁语?,java,string,character,Java,String,Character,我正在写一个pig拉丁语代码,如果单词中的第一个字母是辅音,那么如何让我的程序识别单词中的下一个元音在哪里,这让我大吃一惊。然后它将单词的第一部分,直到第一个元音,移动到单词的末尾,并打印ay。(例如:树木=EEF) 这就是我现在的代码 // word is bringing in the string entered from the user public static void translate(String word) { String wordCap, pigLatin =

我正在写一个pig拉丁语代码,如果单词中的第一个字母是辅音,那么如何让我的程序识别单词中的下一个元音在哪里,这让我大吃一惊。然后它将单词的第一部分,直到第一个元音,移动到单词的末尾,并打印ay。(例如:树木=EEF)

这就是我现在的代码

// word is bringing in the string entered from the user
public static void translate(String word) {

    String wordCap, pigLatin = "";
    char vowels;
    int lowest = 0, tempOne, tempTwo, tempThree, tempFour, tempFive;  

    wordCap = word.toUpperCase();
    vowels = wordCap.charAt(0);

    if (vowels == 'A' || vowels == 'E' || vowels == 'I' || vowels == 'O' || vowels == 'U') {
            word = word + "way";
            System.out.println(word);
        }
        else {
            tempOne = wordCap.indexOf('A', 1);
            if (lowest > tempOne && tempOne != -1) {
                lowest = tempOne;
            }
            tempTwo = wordCap.indexOf('E', 1);
            if (lowest > tempTwo && tempTwo != -1) {
                lowest = tempTwo;
            }
            tempThree = wordCap.indexOf('I', 1);
            if (lowest > tempThree && tempThree != -1) {
                lowest = tempThree;
            }
            tempFour = wordCap.indexOf('O', 1);
            if (lowest > tempFour && tempFour != -1) {
                lowest = tempFour;
            }
            tempFive = wordCap.indexOf('U', 1);
            if (lowest > tempFive && tempFive != -1) {
                lowest = tempFive;
            }




public static char vowel(String word) {
    int start= 0, end= 0;
    char vowels;

    for (int i = 0; i < word.length(); i++) {
        vowels = word.charAt(i);
        if (vowels == 'A' || vowels == 'E' || vowels == 'I' || vowels == 'O' || vowels == 'U') {
            end = i;
            break;
        }
    }
    return (char) end;
}
//word正在引入用户输入的字符串
公共静态无效翻译(字符串字){
字符串wordCap,pigLatin=“”;
字符元音;
int lost=0,tempOne,tentwo,tenthree,tempour,tempove;
wordCap=word.toUpperCase();
元音=wordCap.charAt(0);
如果(元音='A'| |元音=='E'| |元音=='I'| |元音=='O'| |元音=='U'){
单词=单词+方式;
System.out.println(word);
}
否则{
tempOne=wordCap.indexOf('A',1);
如果(最低>临时电话和临时电话!=-1){
最低=tempOne;
}
expndtwo=wordCap.indexOf('E',1);
如果(最低>试探WO&&试探WO!=-1){
最低=最低;
}
TENTHREE=wordCap.indexOf('I',1);
如果(最低值>诱惑值&&诱惑值!=-1){
最低=三分之一;
}
tempFour=wordCap.indexOf('O',1);
如果(最低>tempFour&&tempFour!=-1){
最低=4;
}
tempFive=wordCap.indexOf('U',1);
如果(最低>tempFive&&tempFive!=-1){
最低=5;
}
公共静态字符元音(字符串字){
int start=0,end=0;
字符元音;
for(int i=0;i
(翻译法)


for(int i=0;i您可能希望使用
for
循环遍历每个单词的字母,直到找到元音。例如:

String wordCap = word.toUpperCase();
char vowels;

for (int i=0; i<wordCap.length(); i++) {
    if (isVowel(wordCap.charAt(i))) {
        vowels = wordCap.charAt(i);
        break;
    }
}
然后,您可以在修改单词时引用
voterLindex

if (vowelIndex == 0) {
    newWord = word + "way";
} else {
    newWord = word.substring(vowelIndex) + word.substring(0, vowelIndex) + "ay";
}
return word;

让我试着为您缩短该方法;)

试着这样做:

private static final char[] vowels = {'a', 'e', 'i', 'o', 'u'};
public static String translate(String word) {
    int start = 0; // start index of word
    int firstVowel = 0;
    int end = word.length(); // end index of word
    for(int i = 0; i < end; i++) { // loop over length of word
        char c = Character.toLowerCase(word.charAt(i)); // char of word at i, lower cased
        if(Arrays.asList(vowels).contains(c)) { // convert vowels to a list so we can use List.contains() convenience method.
            firstVowel = i;
            break; // stop looping
        }
    }
    if(start != firstVowel) { // if start is not equal to firstVowel, we caught a vowel.
        String startString = word.substring(firstVowel, end);
        String endString = word.substring(start, firstVowel) + "ay";
        return startString+endString;
    }
    return word; //couldn't find a vowel, return original
}
正如您所看到的,数组大大缩短了时间

如果您对
Arrays.asList().contains()调用感到迂腐,我们可以定义自己的:

public static boolean containsChar(char[] lookIn, char lookFor) {
    boolean doesContainChar = false;
    for(char c : lookIn) {
        if(doesContainChar = c == lookFor)
            break;
    }
    return doesContainChar;
}

哇,非常感谢这是非常有见地的。所以通过使用循环,我真的不需要else-if,是吗?我不这么认为。老实说,我不太明白你甚至试图用这些if语句做什么,尽管我认为这与我给出的for循环的用途有关。非常感谢,这已经超过了我的水平啊哈,没有数组我该怎么做呢?@T3rm3nator,你可以手动分配每个字符,比如
char a='a';
char b='b';
等等,然后手动循环检查每个变量的单词。不过,我建议你熟悉数组,因为它们使循环更容易,从而缩短了代码。让我知道w如果这对你有帮助的话。是的,它有帮助,只是我还没有被教过。到目前为止,这是我所补充的。啊,我能很好地看到差异。这让我很兴奋地了解它们……我收到的每一项作业都让我觉得有一种更简单的方法来做,然后下周我们学习更简单的方法。这让我很紧张,但我可以请看它的用处。非常感谢你的投入。它看起来真的很有用,当我完成它时,我一定会提高投票率!@T3rm3nator,没问题。我希望你今晚能学到一些新东西,正如古语所说:“给人一条鱼,你喂他一天;教人钓鱼,你喂他一辈子。”你的老师/教授正在做正确的事情,向你展示如何用艰难的方式做事,然后向你展示简单的方式节省了多少时间。想想所有可以“使用”dreamweaver,但不能接触html代码的人。
private static final char[] vowels = {'a', 'e', 'i', 'o', 'u'};
public static String translate(String word) {
    int start = 0; // start index of word
    int firstVowel = 0;
    int end = word.length(); // end index of word
    for(int i = 0; i < end; i++) { // loop over length of word
        char c = Character.toLowerCase(word.charAt(i)); // char of word at i, lower cased
        if(Arrays.asList(vowels).contains(c)) { // convert vowels to a list so we can use List.contains() convenience method.
            firstVowel = i;
            break; // stop looping
        }
    }
    if(start != firstVowel) { // if start is not equal to firstVowel, we caught a vowel.
        String startString = word.substring(firstVowel, end);
        String endString = word.substring(start, firstVowel) + "ay";
        return startString+endString;
    }
    return word; //couldn't find a vowel, return original
}
public static String translate(String word) {
    char a = 'a';
    char e = 'e';
    char i = 'i';
    char o = 'o';
    char u = 'u';

    int start = 0;
    int firstVowel = 0;
    int end = word.length();
    for(int i = 0; i < end; i++) {
        char c = Character.toLowerCase(word.charAt(i));
        if(c == a || c == e || c == i || c == o || c == u) {
            firstVowel = i;
            break;
        }
    }
    if(start != firstVowel) {
        String startString = word.subString(firstVowel, end);
        String endString = word.subString(start, firstVowel) + "ay";
        return startString+endString;
    }
    return word;
}
public static boolean containsChar(char[] lookIn, char lookFor) {
    boolean doesContainChar = false;
    for(char c : lookIn) {
        if(doesContainChar = c == lookFor)
            break;
    }
    return doesContainChar;
}