Charat(0)不返回第一个数字,而是第二个数字



piglatin translator。最后,我试图获得第一个元音的位置。索引设置为每个元音的位置,但是使用Pig Latin,您只需要第一个元音的位置即可。当我运行程序时,我并不总是获得第一个元音的位置。它似乎给了我第二个数字,而不是第一个。

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

该示例似乎在if..else条件下具有一些冗余代码。如果要打印第一个元音,则可以使用简单的for循环进行操作,例如:

String word = userWord.next().toLowerCase();
String vowels = "aeiou";
for(int i=0 ; i<word.length() ; i++){
    if(vowels.contains(String.valueOf(word.charAt(i)))){
        System.out.println(word.charAt(i));
        break;
    }
}

请注意,您需要在实际单词上执行toLowerCase才能使contains工作。

我可以看到一些问题,但是可能导致此错误的问题是:

String firstNumber = Integer.toString(index);
firstVowel = Character.toString(firstNumber.charAt(0));

想想这在做什么。首先,您是从索引值中制作String,然后您说第一个元音在该字符串的0索引。

想想这个示例:你好

该程序将运行并分配" 4"到firstNumberfirstVowel,这不是您想要的。

但是,如果您只有一个元音,您的程序将"工作"。

如果您有十个元音会发生什么?我知道这不是一个现实的例子,但说它发生了。您的程序将将最后一个元音的索引分配给firstNumber(例如15(,然后将其第一个字符分配给firstVowel(1(。这根本没有太多意义,是这样,尤其是如果您在索引1中没有元音。

您遇到的单词少于10个字母的主要问题是,您不仅输出第二个数字,还输出了最后一个数字。我想处理的一种方法是浏览代码,并在不确定某个值为什么的情况下放入打印语句。例如,我将另一个打印语句放在您的循环中,告诉您您正在看什么字母,例如:

System.out.println("LETTER: "+ String.valueOf(word.charAt(index)));

这将有助于您避免混乱。做这个问题的正确方法是使用break语句,例如Darshan的答案。另外,您可以使用for循环的属性:

firstVowel = "";
for (int index = 0; index < word.length() && firstVowel == ""; index++) {
CODE
}

请注意,for循环的第二部分是条件语句。您已经知道这可以用来浏览单词的字符,但是您可以在其中插入任何逻辑语句。在此示例中,我将firstVowel的默认值设置为""(将其设置为null是一个faux-pas,但这是另一个故事(。然后,每次循环运行时,都会检查firstVowel的值是否已更改,这当然会在首次通过元音运行时发生。

因此,简而言之,您需要在我的帖子开头修改两行,并且需要找到一种方法,当您找到第一个元音时打破循环。这里提供了一种解决方案,另一个解决方案在Darshan Mehta的帖子中。

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();
                ArrayList<Character> vowels = new ArrayList<>();
                vowels.add('a');
                vowels.add('e');
                vowels.add('i');
                vowels.add('o');
                vowels.add('u');
                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++) {
                        char indchar = word.charAt(index);
                        if (vowels.contains(word.charAt(index))) {
                            System.out.println(index);
                            firstVowel = Character.toString(word.charAt(index));
                            System.out.println(firstVowel);
                            index = word.length();
                        }
                    }
                }
            }
        }

这就是我这样做的方式。我将元音字符串更改为arraylist,因此您可以轻松地检查带有索引的字符串字词中的字符是否为元音,并且代码的工作原理绝对可以。它返回您的索引第一个元音在哪里以及它是什么元音。

相关内容

最新更新