一旦将一个值存储为字符串,您如何循环遍历字符串并将每个值分配给一个字符数组?还必须计算数组中每个元音的出现次数。
这是我当前的代码:
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);
}
}
最佳答案
String str = "stackoveflow";
char[] aa= str.toCharArray();
要直接从字符串中获取一个字符,您可以使用:
str.charAt(i);
关于java - 将字符串更改为 Char 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19918190/