计算字符串中的元音

我从用户那里得到了 3 个字符串作为输入。然后计算每个单词中的元音并打印如下。如果元音计数 = 0 和 1,则打印 0。如果元音计数 = 2,则打印 2。如果元音计数更大大于或等于 3 然后打印 3。我试过这段代码。


Scanner in = new Scanner(System.in);

    String[] str = new String[3];

    for (int i = 0; i<3; i++) 

        str[i] = in .nextLine();

    for (int j = 0; j<3; j++) {

        String s1 = str[j];

        String s = s1.toLowerCase();


        int count = 0;

        for (int i = 0; i<s.length(); i++) 

        {

            if (s.charAt(i) == 'a' || s.charAt(i) == 'e' || s.charAt(i) == 'i' || s.charAt(i) == 'o' || s.charAt(i) == 'u') {

                count++;

            }

            if (s.charAt(i) == ' ') {

                if (count == 0||count == 1) {

                    System.out.print("0");

                } else if (count == 2) {

                    System.out.print("1");

                } else {

                    System.out.print("3");

                } 

                count = 0;

            }


        }

        if (count == 0||count == 1) {

            System.out.println("0");

        } else if (count == 2) {

            System.out.println("1");

        } else {

            System.out.println("3");

        }

    }

但是有一个条件只打印 3 个单词的元音计数,即使用户输入的字符串超过 3 个单词。例如,如果用户给出字符串“hi hello all, how are you, I am fine and u”它仅像这样打印“010 011 001”,但此代码打印为“010 011 00100”。现在我如何更改代码以仅打印 3 个单词而不是超过 3 个单词的元音计数?


爪哇


牛魔王的故事
浏览 92回答 3
3回答

holdtom

第三个词后你需要休息一下。一种选择是创建一个额外的变量来跟踪它并在字符为空格时递增它。3 个词后,您可以打破循环。其他选择是使用方法将字符串拆分为单词split(),并仅迭代前 3 个。

九州编程

我从用户那里得到了 3 个字符串作为输入。然后计算每个单词中的元音并打印如下。如果元音计数 = 0 和 1,则打印 0。如果元音计数 = 2,则打印 2。如果元音计数更大大于或等于 3 然后打印 3。我试过这段代码。Scanner in = new Scanner(System.in);&nbsp; &nbsp; String[] str = new String[3];&nbsp; &nbsp; for (int i = 0; i<3; i++)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; str[i] = in .nextLine();&nbsp; &nbsp; for (int j = 0; j<3; j++) {&nbsp; &nbsp; &nbsp; &nbsp; String s1 = str[j];&nbsp; &nbsp; &nbsp; &nbsp; String s = s1.toLowerCase();&nbsp; &nbsp; &nbsp; &nbsp; int count = 0;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i<s.length(); i++)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (s.charAt(i) == 'a' || s.charAt(i) == 'e' || s.charAt(i) == 'i' || s.charAt(i) == 'o' || s.charAt(i) == 'u') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (s.charAt(i) == ' ') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (count == 0||count == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("0");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (count == 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("1");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("3");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (count == 0||count == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("0");&nbsp; &nbsp; &nbsp; &nbsp; } else if (count == 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("1");&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("3");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }但是有一个条件只打印 3 个单词的元音计数,即使用户输入的字符串超过 3 个单词。例如,如果用户给出字符串“hi hello all, how are you, I am fine and u”它仅像这样打印“010 011 001”,但此代码打印为“010 011 00100”。现在我如何更改代码以仅打印 3 个单词而不是超过 3 个单词的元音计数?

饮歌长啸

您可以使用正则表达式将单词拆分成一个句子,方法如下:String[]&nbsp;splits&nbsp;=&nbsp;myPhrase.split("&nbsp;");拆分句子中的单词,但您必须小心,如果用户输入更多空格,第一个空格将被“消除”,而紧随其后的空格将被拆分。例如:String&nbsp;phrase&nbsp;=&nbsp;"1&nbsp;2&nbsp;&nbsp;3&nbsp;&nbsp;&nbsp;z"; String[]&nbsp;splits&nbsp;=&nbsp;phrase.split("&nbsp;");生成此数组:[1|2| |3| | |z].因此,在我看来,此时您可以使用过滤器,再次遍历数组/列表以消除从正则表达式派生的任何空间,或者更简单地说,当您滚动数组/列表并找到您不考虑的空间时.&nbsp;此时去分析数组/列表的前 3 个元素,丢弃其他元素(可能使用计数器)。最后,再次使用正则表达式,您还可以使用以下命令检查您正在分析的字符是否为元音字母:if&nbsp;(s.charAt(i).matches("[AEIOUaeiou]")){&nbsp;/*it's&nbsp;a&nbsp;vowel*/}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java