检查字符串中元音的方法

时间:2016-11-28 20:59:09

标签: java string methods char

public static void main(String[] args) {
    Scanner Input= new Scanner(System.in);
    System.out.print("Enter String: ");
    String s =Input.nextLine();
    int index = s.length();
    boolean isVowel= true;
    isVowel = vowels(s,index);
    if(isVowel==true)
        System.out.println("Its Vowel");
}
public static boolean vowels(String s,int index){
    String small=s.toLowerCase();
    String large = s.toUpperCase();
    char z=s.charAt(s.length()-1);
    if (s==small) {
        large = s.toUpperCase();
        if(s==large){
        }
        for (int i = 0; i < s.length(); i++) {
            if (z=='A'||z=='E'||z=='I'||z=='O'||z=='U') {
                System.out.println("Character at " + s.charAt(s.length()-1) + " is a vowel");
                return true;
            } else if(z!='A'||z!='E'||z!='I'||z!='O'||z!='U'){
                System.out.println("The String contains no Vowels");    
                return false;
            }
        }
    }
    return true;
    }
}

它不断返回最后一个打印语句,&#34;字符串不包含元音&#34; 有什么建议吗?

1 个答案:

答案 0 :(得分:-1)

import java.util.*;

public class Solution{
    public static void main(String[] args) {
        Scanner Input= new Scanner(System.in);
        System.out.print("Enter String: ");
        String s =Input.nextLine();
        if(vowels(s)) System.out.println("It contains a vowel!");
        else System.out.println("It does not!");
    }
    public static boolean vowels(String s){
        String word = s.toUpperCase();
        char[] words = word.toCharArray();
        for(int i = 0; i<words.length; i++){
            char z = words[i];
            if (z=='A'||z=='E'||z=='I'||z=='O'||z=='U') return true;
        }
        return false;
    }

}

如果我理解你的问题,我认为上面的代码应该这样做。