【问题标题】:How can I get this program to count the vowels in the User's inputted word?我怎样才能让这个程序计算用户输入单词中的元音?
【发布时间】:2017-11-07 01:11:04
【问题描述】:

在我输入的单词中显示元音时有一点问题。我有元音= AEIOU。我在想这可能是问题所在,但我不确定,也不知道如何解决。

这是它给我的:

输入一个单词:ANTARTICA

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

    }
}

【问题讨论】:

  • 如果你想显示你在文本中找到的完全相同的元音,你应该有一个额外的变量来存储元音。例如某种List。然后每次找到元音时,只需将其推入此列表并最终打印出来。

标签: java string for-loop count


【解决方案1】:

当您比较字母时,您必须将两个字母都小写或忽略大小写。我又添加了一种方法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');
    }
}

【讨论】:

    【解决方案2】:

    有很多方法可以做到这一点。

    你可以有一个这样的元音数组:

    char[] vowels = {'a','e','i','o','u'};
    

    然后做一个简单的 for 循环,遍历字符串单词的每个字母:

    int count = 0;
    String vowelsInWord = "";    
    
    for(int i=0; i<word.length; i++){
        for(int j=0; j<vowels.length; j++){
            if(word.charAt(i).toLowerCase() == vowels[j]){
                vowelsInWord += word.charAt(i).toLowerCase(); 
                count++;
            }
        }    
    }
    
    System.out.println("Vowels in word: " + vowelsInWord);
    System.out.println("Vowel Count: " + count);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-07
      • 2021-09-24
      • 1970-01-01
      • 1970-01-01
      • 2018-09-29
      • 1970-01-01
      • 2022-11-01
      • 2020-09-16
      相关资源
      最近更新 更多