[英]I want to add a “counter” to this boolean method
现在,如您所见,我正在试验布尔方法。 我已经通过元音检测学习了charAt方法,但是我不满意。 我想尝试一下,并弄清楚如何为发现的每个元音添加一个计数。
但是,我不确定如何增加每个元音的数量。
用比尔·朗伯(Bill Lumbergh)的睿智话语,如果有人可以帮助我弄清楚如何正确地进行计数,以便每增加一个元音都会增加计数,那将是greeeeeeeeeat。
import java.util.Scanner;
public class Chap10Part4p2 {
public static void main(String[] args) {
String sentence;
int count = 0;
Scanner inputSentence = new Scanner(System.in);
System.out.println("Please enter a sentence.");
sentence = inputSentence.nextLine();
for (int i = 0; i < sentence.length(); i++)
if (isVowel(sentence.charAt(i)))
System.out.print(sentence.charAt(i));
else if (i < sentence.length(); sentence.equals(isVowel);)
count++;
System.out.println(" There are " + count + " vowels in this sentence.");
}
static boolean isVowel(char c) {
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
return true;
else
return false;
}
}
不能100%确定else if语句的目的,它的语法无效。 删除该数字并将其放在下面,该数字将计算总元音。
for(int i = 0; i < sentence.length(); i++) {
if (isVowel(sentence.charAt(i))) {
count++
}
}
System.out.println(" There are " + count + " vowels in this sentence.");
您的代码有几个问题:
else if (i < sentence.length(); sentence.equals(isVowel);)
count
for
循环后有一个{}
,则它的可读性更高for
以便您可以准确地查看循环中有哪些语句。 更换:
for(int i = 0; i < sentence.length(); i++)
if (isVowel(sentence.charAt(i)))
System.out.print(sentence.charAt(i));
else if (i < sentence.length(); sentence.equals(isVowel);)
count++;
System.out.println(" There are " + count + " vowels in this sentence.");
附:
for(int i = 0; i < sentence.length(); i++){
if (isVowel(sentence.charAt(i))){
count++;
System.out.print(sentence.charAt(i));
}
}
System.out.println(" There are " + count + " vowels in this sentence.");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.