检查任何3个辅助辅音

时间:2017-02-20 20:46:48

标签: java string char

我有一个代码,用于检查字符串中是否有三个THE SAME连续辅音。它看起来像这样:

    String cons = "bcdfghjklmnpqrstvwxyz"; 
    char[] consonants = cons.toCharArray();

        int z = 0;
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter your word!");
        String word = scan.next().toLowerCase();
        int length = word.length();
        char[] ch = word.toCharArray();
        for (int i = 0; i < length-2; i++) {
            for (int j = 0; j < 21; j++) {
                if ((ch[i] == consonants[j]) &&
                        (ch[i + 1] == consonants[j])
                        && (ch[i + 2] == consonants[j])) {
                    z++;
                    break;
                }
            }
            if (z == 1)
                break; 
        }
        if (z == 1) {
            System.out.println("Three consecutive consonants here!");
        } else {
            System.out.println("It is fine!");
        }

但我想检查任何连续三个辅音。寻找像圣诞节,乡村,儿童,学校等词。

并且不确定我应该如何更改此代码来执行此操作。你能帮忙吗?

5 个答案:

答案 0 :(得分:1)

在我看来,解决这个问题的最好方法是循环搜索你要按字符搜索的单词。每当您当前所在的字母位于辅音数组中时,请向z添加一个字母。每当您当前所在的字母不在辅音数组中时,请将z设置为零。如果在任何时候z等于3,那么你知道你正在搜索的字符串连续有3个辅音,你就可以摆脱循环。如果你得到循环的结束并且z从未达到三,那么你知道你正在搜索的字符串没有三个连续的辅音。我希望这有帮助,祝你好运!#/ p>

答案 1 :(得分:1)

您应该查看String文档,尤其是contains()方法。

答案 2 :(得分:1)

如果你宣称自己是private static boolean isConsonant(char ch)之类的辅助方法,我认为管理会更容易。此方法将在consonants数组上包含一个循环,返回true的参数与辅音之一相同。

现在,在主程序中,您可以循环显示输入单词的字符,同时保持连续辅音的计数。我将以country为例。你接受c,你的方法应该告诉你它是一个辅音,所以将计数增加到1. o不是,所以重置为零。对u执行相同的操作(不会更改任何内容,但最简单的方法就是这样做)。现在来ntr,所以你的计数会上升到1,然后是2,然后是3.当它达到3时,你已经完成,因为你知道你连续3次辅音。

在您的循环中,您将在if - else语句中调用您的方法,例如if (isConsonant(ch)) ...您将有另一个if语句,以查看计数是否为3 - 在第一个内部或之后。

答案 3 :(得分:1)

您已经询问了如何更改代码以使其正常工作,因此以下内容基于您的代码并且可以正常运行:

String cons = "bcdfghjklmnpqrstvwxyz"; 
char[] consonants = cons.toCharArray();

int z = 0;
Scanner scan = new Scanner(System.in);
System.out.println("Enter your word!");
String word = scan.next().toLowerCase();
int length = word.length();
char[] ch = word.toCharArray();
for (int i = 0; i < length; i++) {
    boolean isConsonant = false;
    for (int j = 0; j < 21; j++) {
        if (ch[i] == consonants[j]) {
            isConsonant = true;
            break;
        }
    }
    if (isConsonant) {
        z++;
        if (z == 3)
            break;
    } else {
        z = 0;
    }
}
if (z == 3) {
    System.out.println("Three consecutive consonants here!");
} else {
    System.out.println("It is fine!");
}

如果它是辅音之一,代码现在通过char检查char。如果是辅音计数器z增加了。一旦计数器达到3,就会退出for循环,每当找到一个不是辅音的字符时,计数器就会重置为零。

但使用正则表达式可以很容易地完成检查:

Scanner scan = new Scanner(System.in);
System.out.println("Enter your word!");
String word = scan.next().toLowerCase();
if (Pattern.compile("[a-z&&[^aeiuo]]{3}").matcher(word).find()) {
    System.out.println("Three consecutive consonants here!");
} else {
    System.out.println("It is fine!"); 
}

[a-z]是从a到z的任何字符,[^aeiuo]是除元音之外的任何字符。两者的交集使用&&完成,结果[a-z&&[^aeiuo]]表示辅音。由于我们正在寻找三个连续的辅音{3},因此会相应添加。

在这里,您可以找到有关正则表达式的详细信息:http://www.regular-expressions.info

答案 4 :(得分:1)

无需使用嵌套for循环。您可以通过使用indexOf()API来检查您的辅音字符串中是否存在该值,从而实现此目的。

    String cons = "bcdfghjklmnpqrstvwxyz"; 


        int z = 0;
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter your word!");
        String word = scan.next().toLowerCase();
        //int length = word.length();
        char[] ch = word.toCharArray();
        for (int i = 0; i < ch.length-2; i++) {

                if (
                        cons.indexOf(ch[i])!=-1 &&
                        cons.indexOf(ch[i+1])!=-1 &&
                        cons.indexOf(ch[i+2])!=-1
                   ) 
                {
                    z++;
                    break;
                }
              if (z == 1)
                break; 
        }
        if (z == 1) {
            System.out.println("Three consecutive consonants here!");
        } else {
            System.out.println("It is fine!");
        }
        }