一旦将值存储为字符串,如何遍历字符串并将每个值分配给 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);
}
}