2016-11-28 35 views
-4
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; 
    } 
} 

它一直返回最後一個打印語句「字符串不包含元音」 有什麼建議嗎?檢查字符串中元音的方法

+0

請格式化您的代碼並正確解釋您的問題。閱讀http://stackoverflow.com/help/how-to-ask – StackFlowed

+0

你想要的輸出是什麼? –

+1

逐句通過您的代碼,我們不是一個調試服務。 –

回答

-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; 
    } 

} 

如果我理解你的問題的權利,我認爲上面的代碼應該這樣做。

相關問題