有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将一个句子从英语翻译成拉丁语

import java.util.*;
public class Lab04D
{
    public static boolean startsWithVowel (String a)
{
    String VOWELS = "aeiouAEIOU";
    return (VOWELS.indexOf(a) != -1);
}

public static int findVowel(String str)
{
    for (int i = 0; i < str.length(); i++) 
        {
        if (startsWithVowel(str.charAt(i))) 
            {
            return i;
        }
    }
    return -1;
}

public static String piggy (String str)
{
    int firstVowel = findVowel(str);

    if (firstVowel <= 0) {
        return str + "way";
    }
    else {
        return str.substring(firstVowel, str.length()) + 
               str.substring(0,firstVowel) + "ay";
    }
}   
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Please enter a sentence with 5 words:");
String str = input.nextLine();

//This is where I seperate the sentence into strings

    int space = str.indexOf(" ");
    String s1 = str.substring(0,space);
    String s2 = str.substring(space+1);
    int space2 = str.indexOf(" ");
    String s3 = str.substring(0,space2);
    String s4 = str.substring(space2+1);
    int space3 = str.indexOf(" ");
    String s5 = str.substring(0,space3);
    String s6 = str.substring(space3+1);
    int space4 = str.indexOf(" ");
    String s7 = str.substring(0,space4);
    String s8 = str.substring(space4+1);

System.out.println("                         My Pig Latin Translator");
System.out.println("                            by: Student Name Here");

System.out.println("Your original sentence is:"+str);
System.out.println("Your pig latin is: "+(piggy(s1))+" "+(piggy(s2))+" "+(piggy(s3))+" "+(piggy(s4)));

}
 }

我试图翻译这句话,但当我输入示例句“pig latin is so cool”时,我的输出是“igpay atinlay is so cool”。我能做些什么来区分这些单词,以便每个单词都能被翻译


共 (0) 个答案