Java元音计数器的问题

Java元音计数器的问题,java,for-loop,Java,For Loop,我很难让我的元音计数器在我为这个cap创建的for循环中运行。这是我的密码: //java app that counts vowels using a forloop import java.util.Scanner; public class App { public static void main(String[] args) { Scanner ent=new Scanner(System.in); String string1; System.out.println("

我很难让我的元音计数器在我为这个cap创建的for循环中运行。这是我的密码:

//java app that counts vowels using a forloop
import java.util.Scanner;
public class App {

public static void main(String[] args) {
Scanner ent=new Scanner(System.in);     
String string1; 
System.out.println("Entered a letter:");
string1 = ent.nextLine();}


public static int numberVowels(String string1){
    int count=0;
    int vowels=0;
    int consonants=0;
    for(int i=0; i<string1.length(); i++){

        char ch=string1.charAt(i);
        if(ch=='a' || ch=='e' ||ch=='i' ||ch=='o'||ch=='u' ){

            vowels++;
            return ch=ch+vowel;
        }else{

            consonants++;
        }
    }

}
//使用forloop计算元音的java应用程序
导入java.util.Scanner;
公共类应用程序{
公共静态void main(字符串[]args){
扫描仪ent=新扫描仪(System.in);
弦1;
System.out.println(“输入一个字母:”);
string1=ent.nextLine();}
公共静态int numberpowers(字符串string1){
整数计数=0;
int元音=0;
int辅音=0;

对于(int i=0;i您的
if
中有一个返回语句(我觉得这很似是而非),您需要一个保证可访问的;因为您的方法是计算
元音
,所以您应该坚持这一点。最后,因为您只测试小写元音,所以我建议在测试之前调用
toLowerCase
。例如

public static int numberVowels(String string1) {
    int vowels = 0;
    for (int i = 0; i < string1.length(); i++) {
        char ch = Character.toLowerCase(string1.charAt(i));
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
            vowels++;
        }
    }
    return vowels;
}

是,消息是关于Java语法的,请尝试使用以下方法进行修复:

如果您的方法具有返回类型,则始终检查已返回变量的Java。

`

//使用forloop计算元音的java应用程序 导入java.util.Scanner; 公共类应用程序{ 公共静态void main(字符串[]args){ 扫描仪ent=新扫描仪(System.in); 弦1; System.out.println(“输入一个字母:”); string1=ent.nextLine();} 公共静态int numberpowers(字符串string1){ 整数计数=0; int元音=0; int辅音=0; char tmp='';
对于(int i=0;当然,辅音等于字符串长度减去元音值现在,当我想让程序知道我打印了一个元音时,它不会打印任何内容:char ch=string1.charAt(i);if(ch='a'| ch='e'| ch='i'| ch='o'| ch='u'){voterls++;tmp=ch;System.out.println(“您打印出了一个元音”);}否则{consonants++;}显示我也在main方法中调用了我的方法
public static int numberVowels(String string1) {
    int vowels = 0;
    for (char ch : string1.toLowerCase().toCharArray()) {
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
            vowels++;
        }
    }
    return vowels;
}
´

//java app that counts vowels using a forloop
import java.util.Scanner;
public class App {

public static void main(String[] args) {
Scanner ent=new Scanner(System.in);     
String string1; 
System.out.println("Entered a letter:");
string1 = ent.nextLine();}


public static int numberVowels(String string1){
    int count=0;
    int vowels=0;
    int consonants=0;
    char tmp='';
    for(int i=0; i<string1.length(); i++){

        char ch=string1.charAt(i);
        if(ch=='a' || ch=='e' ||ch=='i' ||ch=='o'||ch=='u' ){

            vowels++;
            tmp= ch=ch+vowel;
        }else{

            consonants++;
        }
    }
return tmp;
}