java - 如何让这个程序计算用户输入单词中的元音?

标签 java string for-loop count

我在显示输入的单词中的元音时遇到了一个小问题。我有元音= AEIOU。我想也许这就是问题所在,但我不确定,也不知道如何解决它。

这就是它给我的:

输入一个词:南极洲

以下是 ANTARTICA 中的元音:AEIOU

元音数量:4

流程已完成。

正如你所看到的,它计算了元音,我只是想让它们也显示出来。我该怎么做呢? (我是一个菜鸟编码员......)

import static java.lang.System.*;
import java.util.*;

public class Java2106{
    public static void main(String[] args)  {
        new Solution();
}}


class Solution
{
    String word;
    int count;
    String vowels;

    Solution()
    {
        input();
        output();
    }

    public void input()
    {
        Scanner scan = new Scanner(in);

        out.print("Enter a word: ");
        word = scan.nextLine();


    }

    public void output()
    {
        vowels = "AEIOU";
        count = 0;

        out.println();
        out.print("Here are the vowels in " + word + ": " + vowels);


        for (int x = 0; x < word.length(); x++)
        {
            String let = word.substring(x,x+1);

            if (vowels.contains(let))
                count++;
        }

        out.println("\nVowel count: " + count);

    }
}

最佳答案

当您比较字母时,您必须将两个字母都小写或忽略大小写。我又添加了一种方法 java8(),它向您展示了如何使用 Java 8 lambda 函数解决此问题。

为了您自己的利益,我强烈建议您阅读基础 Java 并学习如何编写 Java 类,因为下面的类不是一个好的实现。对于像这样的简单程序来说,您的逻辑太复杂了,因此我强烈建议您回到基础知识。

import static java.lang.System.in;
import static java.lang.System.out;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Java2106 {
    public static void main(String[] args) {
        new Solution();
    }
}

class Solution {
    String word;
    int count;
    String vowels;

    Solution() {
        input();
        output();
        java8();
    }

    private void java8() {
        Scanner scan = new Scanner(in);
        out.print("Enter a word: ");
        word = scan.nextLine();
        List<Character> letters = convert(word.toLowerCase());
        letters.removeIf(c -> (c.charValue() != 'a' && c.charValue() != 'e' && c.charValue() != 'i' && c.charValue() != 'o' && c.charValue() != 'u'));
        System.out.println("Vowels in given word: " + letters.size());
        scan.close();
    }

    private List<Character> convert(String word) {
        List<Character> letters = new ArrayList<Character>();
        for (Character c : word.toCharArray()) {
            letters.add(c);
        }
        return letters;
    }

    public void input() {
        Scanner scan = new Scanner(in);
        out.print("Enter a word: ");
        word = scan.nextLine();
    }

    public void output() {
        vowels = "AEIOU";
        count = 0;
        out.println();
        out.print("Here are the vowels in " + word + ": " + vowels);
        for (int x = 0; x < word.length(); x++) {
            String let = word.substring(x, x + 1);
            if (vowels.toLowerCase().contains(let.toLowerCase()))
                count++;
        }

        out.println("\nVowel count: " + count);

    }
}

简单的实现之一:

import java.util.Scanner;

public class Java2106 {
    public static void main(String[] args) {
        int count = 0;
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a word: ");
        String word = scan.nextLine();
        for (char c : word.toLowerCase().toCharArray()) {
            if (isVowel(c)) {
                count++;
            }
        }
        System.out.println("Total Vowels : " + count);
        scan.close();
    }

    private static boolean isVowel(char c) {
        return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
    }
}

关于java - 如何让这个程序计算用户输入单词中的元音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47148395/

相关文章:

Java:多线程内的 XA 事务传播

r - 给定一个字符串列表,如何转换回数字列表?

PHP "if"问题

java - 为什么 if block 不针对最终情况运行?

java - 如何在android中循环遍历TextView变量

java - 具有两种数据类型的 JTable 单元格

java - 来自服务实现的 Google Web Toolkit 异步调用

Java字符串分割错误

Javascript 蠕虫游戏 Canvas 问题

java - Gson 可以生成 XML 吗?