3

一旦将值存储为字符串,如何遍历字符串并将每个值分配给 char 数组?还必须计算数组中每个元音的出现次数。

这是我当前的代码:

public class Part1_5 {

  /**
   * Method that gets user name and stores it as a string. Each value then
   * assign to a char array. no of vowels are counted and no of each printed
   */
  public static void main(String[] args) {

    // Setting up scanner
    Scanner scanner = new Scanner(System.in);

    // declaring string for name
    String userName = null;

    // declaring ints to hold total no of each vowel
    int totalOfA = 0;
    int totalOfE = 0;
    int totalofI = 0;
    int totalofO = 0;
    int totalofU = 0;

    // Get user input for name
    System.out.println("Please enter your Name...");
    userName = scanner.nextLine();

    for (int loop = 0; loop < userName.length(); loop++) {

      // declaring char array
      char[] letter = userName.toCharArray();

      if (userName.charAt(0) == 'a') {

        totalOfA++;

      }

    }

    System.out.println(totalOfA);

  }

}
4

4 回答 4

2
String str = "stackoveflow";
char[] aa= str.toCharArray();

要直接从字符串中获取字符,您可以使用:

str.charAt(i);
于 2013-11-11T23:51:59.313 回答
2

迭代(并计算)字符串中的所有字符怎么样?

元音的计数是否必须区分大小写?

Map<Character, Integer> count = new HashMap<Character, Integer>();
char[] chars = "this is a test".toCharArray();
for (char curr : chars){
    Integer tmp = count.get(curr);
    if (tmp == null){ tmp = new Integer(0); }
    tmp++;
    count.put(curr, tmp);    
}
System.out.println(count.get("a".charAt(0)));
System.out.println(count.get("e".charAt(0)));
System.out.println(count.get("i".charAt(0)));
System.out.println(count.get("o".charAt(0)));
System.out.println(count.get("u".charAt(0)));

这给你...

1
1
2
null
null

处理 null 是微不足道的 - 例如 result == null ?0:结果

编辑:改进了 100% 以上的不区分大小写!!

Map<Character, Integer> count = new HashMap<Character, Integer>();
for (char curr :  "this IS a test".toLowerCase().toCharArray()){
    Integer tmp = count.get(curr);
    count.put(curr, tmp == null ? 1 : ++tmp);
}

同样的事情,但在 Groovy 中......

def count = "this IS a test".toLowerCase().collect().countBy{it}
于 2013-11-11T23:55:20.913 回答
0

使用 中的计数变量for loop来指定在 中的位置userName String以向上计数元音。

此外,你甚至不需要char array你正在做的方法。但是如果你确实需要一个,你应该在开始之前定义它for loop。为什么要创建这么多次?

您询问了如何遍历String并将每个值分配给 a char array,但您不需要这样做:您可以简单地做您所做的,char[] letter = userName.toCharArray();.

public class Part1_5 {
    public static void main(String[] args) {

        // Setting up scanner
        Scanner scanner = new Scanner(System.in);

        // declaring string for name
        String userName = null;

        // declaring ints to hold total no of each vowel
        int totalOfA = 0,totalOfE = 0,totalofI = 0,totalofO = 0,totalofU = 0;

        // Get user input for name
        System.out.println("Please enter your Name...");
        userName = scanner.nextLine();

        // declaring char array (declare it once, before the loop)
        char[] letter = userName.toCharArray();

        for (int loop = 0; loop < userName.length(); loop++) {

            // check and count for any vowel at every iteration of the loop
            if (userName.charAt(loop) == 'a')
                totalOfA++;
            else if (userName.charAt(loop) == 'e')
                totalOfE++;
            else if (userName.charAt(loop) == 'i')
                totalOfI++;
            else if (userName.charAt(loop) == 'o')
                totalOfO++;
            else if (userName.charAt(loop) == 'u')
                totalOfU++;
        }
        System.out.println(totalOfA);
    }
}
于 2013-11-11T23:46:11.703 回答
0
if (userName.charAt(loop) == 'a') {
于 2013-11-11T23:43:02.823 回答