嗨,我应该在这段代码中添加什么来使它在用户输入字符串时也显示多少辅音。 [英] Hi, what should I add in this code to make it also show how many consonants when the user input a string.

查看:95
本文介绍了嗨,我应该在这段代码中添加什么来使它在用户输入字符串时也显示多少辅音。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.util.Scanner;

public class NewClass {
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Input the string: ");
        String str = in.nextLine();
        System.out.print("Number of Vowels in the string: "
        + count_Vowels(str)+"\n");
    }
    public static int count_Vowels(String str) 
    {
        int count = 0; for (int i = 0; i < str.length(); i++)
        {
            if (str.charAt(i) == 'a' || str.charAt(i) == 'e' ||
            str.charAt(i) == 'i' || str.charAt(i) == 'o' || str.charAt(i)
            == 'u')
            {
                count++;
            }
        }
        return count;
    }
}





我的尝试:



i尝试使用if else,但它不会运行。如果有人可以提供帮助,我真的很感激。



What I have tried:

i have tried using "if else" but it wont run. i really appreciate if somebody can help.

推荐答案

提示1:'A','E','我','O', 'U'也是一个元音,但你的代码无法检测到它们。

提示2:如果一个字符是一个字母(见 Java.lang.Character.isLetter()方法 [ ^ ])然后,如果它不是元音,则必须是辅音
Hint 1: 'A','E','I','O','U' are a vowels as well, but your code is not able to detect them.
Hint 2: if a character is a letter (see Java.lang.Character.isLetter() Method[^]) then, if it isn't a vowel, it must be a consonant.


创建一个名为 count_Consonants 的新函数,并将测试更改为仅允许计算辅音。



但是这种设计很差:首先,它不能正常工作:所有的猫都是猫应该返回7,但是这将返回6,因为它不适用于大写字母。 />
其次,因为对辅音使用相同的逻辑意味着可怕的长如果条件。

而是创建两个字符数组:一个包含aeiou,另一个包含所有其他小写字母。



现在写一个名为 count_Matches 的函数接受一个字符数组,并检查一个字符串并返回匹配的数量。称之为两次:一次来自 count_Vowels ,一次来自 count_Consonants

写另一个名为<的函数code> isAMatch 它接受一个字符数组和一个字符。

isAMatch 函数内,转换字符小写并循环遍历数组寻找它。如果你发现它,返回true。如果你在没有找到它的情况下得到数组,请返回false。

count_Matches 函数内,循环遍历字符串中的每个字符就像你现在一样,但是使用 isAMatch 函数检查每个角色并计算它。



它是很简单,它允许你检查数字或特殊字符(标点符号等),将来只需要很小的改动。



但是...这个是你的作业,所以我不会给你任何代码!
Create a new function named count_Consonants and change the test to only allow consonants to be counted.

But that design is poor: first off, it doesn't work properly: "All cats are felines" should return 7, but that will return 6 because it doesn't work with uppercase letters.
Secondly, because using the same logic for consonants means a horribly long if condition.
Instead, create two arrays of characters: one contains "aeiou" and the other all the other lowercase letters.

Now write a function called count_Matches that accepts an array of characters, and a string to check and returns the number of "matches". Call it twice: once from count_Vowels and once from count_Consonants
Write another function called isAMatch which takes a character array and a character.
Inside the isAMatch function, convert the character to lowercase and loop through the array looking for it. If your find it, return true. if you get to teh eend of teh array without finding it, return false.
Inside the count_Matches function, loop through each character in the string just as you are at the moment, but use the isAMatch function to check each character and count it.

It's pretty simple to do, and it allows you to check for numbers, or special characters (punctuation etc.) with minimal changes in the future.

But ... this is your homework, so I'll give you no code!


这篇关于嗨,我应该在这段代码中添加什么来使它在用户输入字符串时也显示多少辅音。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆