【问题标题】:Creating an array that counts vowels in a string创建一个计算字符串中元音的数组
【发布时间】:2015-10-29 18:21:05
【问题描述】:

我想创建一个数组来搜索字符串中的元音 像嗯

public class letstrythis{
    public static void main(string[]arg){
        String[] myStringArray = [1];
        String[] myStringArray = {"iwanttotryifthisworks"};
        String myStringArray = new String[] {"iwanttotryifthisworks};

        if(string=a,e,i,o,u;
                for (int i=0;i< string.length; i++){
                int intvalue = string[i];
                system.out.println(i);
                )
    }

}

问题,在字符串中搜索元音的函数是什么?

【问题讨论】:

  • 欢迎来到 StackOverflow!关于你的问题的几件事。首先,现在很难阅读,因为您的代码根本没有格式化。其次,你的代码不会编译——它有很多语法错误,老实说,它看起来更像伪代码而不是 Java。在 SO,我们帮助有具体实施问题的人 - 尝试为自己编写答案,如果遇到问题,请回来并 ask a good question。我们很乐意为您提供帮助。

标签: java string for-loop


【解决方案1】:
    public class CountVowels {
            public static void main(String[] args) {
                    String string = "this is a test string for counting vowels";
                    int count = 0;
                    for (int i = 0;i < string.length();i++) {
                            if (isVowel(string.charAt(i)))
                                    count++;
                    }
                    System.out.println(count);
            }
            public static boolean isVowel(char ch) {
                    return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
            }
    }

【讨论】:

  • 所以 i 是第一个 0 然后对于每个字母它检查 char 值 a e i o u?非常感谢!
  • 很高兴我的回答对您有所帮助。 :)
【解决方案2】:

遍历数组并在看到元音时增加计数。您可以将所有元音保存在一个集合中。没有内置函数可以执行此操作。

Set<Character> vowels = new HashSet<Character>();

vowels.add('a');vowels.add('e');vowels.add('i');vowels.add('o');vowels.add('u');

String input = new String("any input string");
int count=0;

for (int i=0 ; i<input.size() ; i++)
{
    if(vowels.contains(input.charAt(i)))
      count++;
}

System.out.println(count);

或使用字符数组

Set<Character> vowels = new HashSet<Character>();

vowels.add('a');vowels.add('e');vowels.add('i');vowels.add('o');vowels.add('u');

String inp = new String("any input string");

char[] input = inp.toCharArray();

int count=0;

for (int i=0 ; i<input.length ; i++)
{
    if(vowels.contains(input[i]))
      count++;
}

System.out.println(count);

【讨论】:

  • 编译后报错 vowels.java:4: error: cannot find symbol Set vowels = new HashSet(); ^ 符号:class 设置位置:class vowels
  • 您可能需要导入 Set 和 HashSet 类。 import java.util*;
  • 是的,您需要导入。如果您使用的是 IDE,它应该会自动提示您。
  • 我正在使用jgrasp,这是我学习的程序,我添加了导入但仍然错误。
  • 您使用的是哪个版本的 Java?
【解决方案3】:
import java.util.Scanner;

public class Countingvowel {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        char letter;
        String sentence = "";
        System.out.println("Enter a character for which to search");
        letter = in.next().charAt(0);
        System.out.println("Enter the string to search");
        sentence = in.next();

        int count = 0;
        for (int i = 0; i < sentence.length(); i++) {
            char ch = sentence.charAt(i);
            if (ch == letter) {
                count++;
            }
        }
        System.out.printf("There are %d occurrences of %s in %s", count, letter, sentence);

    }
}

【讨论】:

  • 我想让值默认为元音,而不是输入元音
  • 所以只需使用 Set 来保存元音
  • 除了使用set还有什么办法吗?
  • ArrayList 类似
  • 好的,所以你希望用户输入一个字符串。然后你想检查元音。你想检查所有的元音而不是其他字符吗?
【解决方案4】:

如果你不想循环,你总是可以使用正则表达式。

导入 java.util.regex.Matcher; 导入 java.util.regex.Pattern;

public class Regex {

    public static void main(String[] args) {
        Pattern p = Pattern.compile("[aeiou]", Pattern.CASE_INSENSITIVE);

        String s = "iwanttotryifthisworks";

        Matcher m = p.matcher(s);

        int i = 0;
        while (m.find()) i++;

        System.out.println("Matches: " + i);
    }

}

m.find() 有一个循环,但不是一堆循环。

【讨论】:

    猜你喜欢
    • 2020-02-19
    • 2011-09-05
    • 2021-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-07
    • 2013-11-26
    相关资源
    最近更新 更多