这是我的代码,我试图找到字符串vowelCount和keepng轨道中有多少元音在int count [];所以字符串是(“black banana republic boots”),输出应该是{4,1,1,2,1}。我的数学是否正确。如果不是什么错误
class lab204 {
public static String vowelCount() {
String vowelCount[] = ("black banana republic boots");
int count[];
int counter = 0;
for( int i=0; i<s.length(); i++ ) {
if( vowelCount.charAt(i) == 'a' ) {
counter++;
}
if( vowelCount.charAt(i) == 'e' ) {
counter++;
}
if( vowelCount.charAt(i) == 'i' ) {
counter++;
}
if( vowelCount.charAt(i) == 'o' ) {
counter++;
}
if( vowelCount.charAt(i) == 'u' ) {
counter++;
}
}
}
}
答案 0 :(得分:2)
首先,您的代码将无法编译。这是因为您正在错误地创建数组。请注意,您不需要数组,除非您想一次计算多个字符串。
public static String vowelCount() {
//Does not need to be an array
String sentence = "black banana republic boots";
//Create your array to count 5 vowels
int[] vowelCount = { 0, 0, 0, 0, 0 };
for( int i = 0; i < sentence.length(); i++ ) {
//Don't forget to check for uppercase too!
if( sentence.charAt(i) == 'a' || sentence.charAt(i) == 'A' ) {
vowelCount[0]++;
}
if( sentence.charAt(i) == 'e' || sentence.charAt(i) == 'E' ) {
vowelCount[1]++;
}
if( sentence.charAt(i) == 'i' || sentence.charAt(i) == 'I' ) {
vowelCount[2]++;
}
if( sentence.charAt(i) == 'o' || sentence.charAt(i) == 'O' ) {
vowelCount[3]++;
}
if( sentence.charAt(i) == 'u' || sentence.charAt(i) == 'U' ) {
vowelCount[4]++;
}
}
}
我假设你对数组知之甚少,所以我建议你阅读here。
此外,您的函数被声明为返回一个字符串,当您真的想要返回带有元音数的数组时。像这样改变你的函数声明:
public static int[] vowelCount() {
在函数结束时,在for循环之后,返回vowelCount
数组:
public static int[] vowelCount() {
String sentence = "black banana republic boots";
int[] vowelCount = { 0, 0, 0, 0, 0 };
for (int i = 0; i < sentence.length(); i++) {
//If statements
//...
} //End of for loop
//Return the array counting the vowels
return vowelCount;
} //End of function
答案 1 :(得分:0)
请填写空白
public static String vowelCount() {
String s = ("black banana republic boots"); //test string
int count[] = new int[__]; //to use an int array for counting
for( int i=0; i<s.length(); i++ ) { //looking at test string, one char at a time
if( s.charAt(i) == 'a' ) {
count[0]++; //increment the first array element
}
if( s.charAt(i) == 'e' ) {
___________ //increment the second array element
}
if( s.charAt(i) == 'i' ) {
___________ //...
}
if( s.charAt(i) == 'o' ) {
__________ //...
}
if( s.charAt(i) == 'u' ) {
_____________
}
}
}
答案 2 :(得分:-1)
有些观点:
1)如果有其他行,则不需要{}括号
2)以下是您的情况的示例代码
public class VowelTester {
char[] vowels = {'a','e','i','o','u'};
/** Check for vowels
* @param sample
* @return array containing the counter of each vowel [a,e,i,o,u]
*/
public int[] checkForVowels(String sample) {
int[] array = new int[5];
for (int i = 0; i < sample.length(); i++) {
if (sample.charAt(i) == 'a')
array[0] += 1;
if (sample.charAt(i) == 'e')
array[1] += 1;
if (sample.charAt(i) == 'i')
array[2] += 1;
if (sample.charAt(i) == 'o')
array[3] += 1;
if (sample.charAt(i) == 'u')
array[4] += 1;
}
System.out.println("a:"+array[0]+" e:"+array[1]+" i:"+array[2]+" o:"+array[3]+" u:"+array[4]);
return array;
}
public static void main(String[] args){
new VowelTester().checkForVowels("Find out how many vowels i have");
}
}