简体   繁体   English

用于在数组中打印元音的Java程序?

[英]Java program to print vowels in an array?

I am writing a program to find the number of vowels in each word within an array. 我正在编写一个程序来查找数组中每个单词中的元音数量。 I have the code finished, I am just unsure of what the return value should look after the if statement. 我已经完成了代码,我只是不确定if语句后的返回值应该是什么。

public static int vowels(char [] array, int x, int y) {
    if(array[x]=='a' || array[x]=='e' || array[x]=='i' || array[x]=='o' || 
                                         array[x]=='u' || array[x]=='y') {
        y++;
    }
    if (x < array.length) {
        x++;
        vowels (array, x, y);
        return ???;
    } else {
        return y;
    }
}

Instead of 代替

vowels (array, x, y);
return ???;

You should return the value, computed by vowels, since it is a clean function, and doesn't has side effects: 你应该返回由元音计算的值,因为它是一个干净的函数,并且没有副作用:

return vowels (array, x, y);

If I understand you correctly- 如果我理解你 -

private static String vowelString = new String("aeiou");
public static int vowels(char [] array) {
        int x=0;
        for(char ch:array) {
            if(vowelString.indexOf(ch)>=0)
                x++;
        }
        return x;
    }

Using recursion here it is a very ugly solution. 在这里使用递归是一个非常难看的解决方案。 It would be much more simpler, if you rewrite it so: 如果你重写它会更简单:

public static int vowels(char[] array) {
    int y = 0;
    for (char x : array) {
        if (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u' || x == 'y')
            y++;
    }
    return y;
}

But if you actually need use recursion (eg it is your homework, and it is your task, that you must use it), then you need to fix some mistakes in your code and remove y argument - it is unnecessary: 但是如果你真的需要使用递归(例如它是你的功课,这是你的任务,你必须使用它),那么你需要修复你的代码中的一些错误并删除y参数 - 这是不必要的:

public static int vowels(char[] array, int x) {
    int y = 0;
    if (array[x] == 'a' || array[x] == 'e' || array[x] == 'i' || array[x] == 'o' ||
            array[x] == 'u' || array[x] == 'y')
        y++;
    if (x < array.length - 1) {
        x++;
        y += vowels(array, x);
    }
    return y;
}
  • First, I would prepare a map of vowels. 首先,我会准备一个元音地图
  • Second, I would iterate over my array and check each element whether they exist in the map. 其次,我将迭代我的数组并检查每个元素是否存在于地图中。 If they exist, update the counter. 如果存在,请更新计数器。 Otherwise, continue to check other elements. 否则,继续检查其他元素。

Sample code: 示例代码:

Map<String, String> vowels = new HashMap<String, String>() {
    {
        put("a", "");
        put("e", "");
        put("i", "");
        put("o", "");
        put("u", "");
        put("y", "");
    }
};

String input = "blablabla";
int counter = 0;

for (int i = 0; i < input.length(); i++) {
    if (vowels.containsKey(input.charAt(i) + ""))
        counter++;
}

Here's the solution to the program - 这是该计划的解决方案 -

public class so { public static void main(String[] args) { // TODO Auto-generated method stub public class so {public static void main(String [] args){// TODO自动生成的方法存根

    char array[] = {'a', 'e', 'i', 'x', 'h', 't', 'd' };
    System.out.print( vowels(array, 0, 0));
}

public static int vowels(char [] array, int x, int y) {
    if(array[x]=='a' || array[x]=='e' || array[x]=='i' || array[x]=='o' || 
                                         array[x]=='u' || array[x]=='y') {
        y++;
    }

Note that in x should be less than array.length-1 since its value is getting incremented. 请注意,x中的值应小于array.length-1,因为它的值会增加。 Otherwise it will throw an index out of bounds exception . 否则它将抛出索引超出范围的异常

    if (x < (array.length-1)) {
        x++;
        return vowels (array, x, y);

    } else {
        return y;
    }
    //return -1;
   }
}

Cheers, 干杯,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM