明胶翻译器。最后,我试图获得第一个元音的位置。索引被设置为每个元音的位置,但对于拉丁语,只需要第一个元音的位置。当我运行程序时,我并不总是得到第一个元音的位置。它似乎给了我第二个数字,而不是第一个。charAt(0)不返回第一个数字,而是第二个
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Assignment_4_Piglatin {
public static void main(String[] args) {
Scanner userWord = new Scanner(System.in);
System.out.println("K. Caleb Swallow");
System.out.println("Welcome to the Pig Latin Translator!");
boolean run = true;
while (run) {
System.out.println("Please enter a word(or press Q to quit):");
String firstLetter = "something";
String firstVowel = "test";
String word = userWord.next();
String vowels = "aeiou";
if (word.equals("Q")) {
System.exit(0);
}
firstLetter = Character.toString(word.charAt(0));
if (firstLetter.equals("a") || firstLetter.equals("e") || firstLetter.equals("i") || firstLetter.equals("o") || firstLetter.equals("u")) {
System.out.println(word + "way");
} else {
for (int index = 0; index < word.length(); index++) {
if (vowels.contains(String.valueOf(word.charAt(index)))) {
System.out.print(index);
String firstNumber = Integer.toString(index);
firstVowel = Character.toString(firstNumber.charAt(0));
}
}
}
System.out.println(firstVowel);
尝试格式化您的代码,然后发布到此处(例如http://www.freecodeformat.com/java-format.php) –
是否将大写字母视为您程序的有效输入? – MrMister
在找到第一个元音之后添加一个break语句,以便for循环停止迭代。 –