【发布时间】:2020-06-13 20:14:47
【问题描述】:
我正在为课程制作一个程序,它可以打印出单词中元音的数量,如果有任何帮助,我们将不胜感激。目前,程序打印出正确数量的元音,但之前多次打印出打印语句“vowels:”。我试过移动打印语句和大括号,但它说“错误:'else if'没有'if'”。如果解决方案显而易见,我对 Java 完全陌生,很抱歉。提前谢谢你:)
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter text: ");
String text = input.nextLine();
text = text.toLowerCase();
int vowels= 0;
int l;
l= text.length();
for (int i = 1; i < text.length(); i++) {
String wordPRT = text.substring(i,i+1);
if (wordPRT.compareToIgnoreCase("a")==0 || wordPRT.compareToIgnoreCase("e")==0||
wordPRT.compareToIgnoreCase("i")==0
|| wordPRT.compareToIgnoreCase("o")==0
|| wordPRT.compareToIgnoreCase("u")==0){
vowels++;
System.out.println("vowels: " + vowels);
}
else if(vowels<1){
System.out.print("no vowels");
}
}
}
}
【问题讨论】: