2016-12-02 27 views
-3

我想完成這個項目,我不知道如何在我的其他方法中使用我現有的方法。我想擺脫VOWELS,它被定義爲一個類的字段,我只想使用方法isVowel,在輸入Char後返回boolean使用現有的方法,而不是新的參數

這是我有:

public class StringAndIO { 

    private static Scanner v; 
    static final String VOWELS = "AaEeIiOoUuÄäÖöÜü"; 

    public static boolean isVowel(char c) { 
     if (c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i' || c == 'I' || c == 'o' || c == 'O' || c == 'u' 
      || c == 'U' || c == 'ä' || c == 'Ä' || c == 'ö' || c == 'Ö' || c == 'ü' || c == 'Ü') { 
      return true; 
     } else { 
      return false; 
     } 
    } 

    public static String toPigLatin(String text) { 
     String ret = ""; 
     String vowelbuf = ""; 
     for (int i = 0; i < text.length(); ++i) { 
      char x = text.charAt(i); 
      if (VOWELS.indexOf(x) != -1) { 
       vowelbuf += x; 
      } else { 
       if (vowelbuf.length() > 0) { 
        ret += vowelbuf + "b" + vowelbuf + x; 
        vowelbuf = ""; 
       } else { 
        ret += x; 
       } 
      } 
     } 
     if (vowelbuf.length() > 0) { 
      ret += vowelbuf + "b" + vowelbuf; 
     } 
     return ret; 
    } 

    /** 
    * only there for testing purpose 
    */ 
    public static void main(String[] args) { 
     v = new Scanner(System.in); 
     System.out.println("Enter a Char!"); 
     char c = v.next().charAt(0); 
     System.out.println(isVowel(c)); 
     String s = "Meine Mutter ißt gerne Fisch"; 
     System.out.println(s); 
     System.out.println(toPigLatin(s)); 
     System.out.println(); 
    } 
} 
+2

裏面你isVowel(x)方法請把你的時間,使代碼可讀性我們。這也不是JavaScript。 –

+2

爲什麼標記爲「JavaScript」? – nmnsud

回答

0

這是如何使用其他方法

public static String toPigLatin(String text) { 


    String ret = ""; 
    String vowelbuf = ""; 


    for (int i = 0; i < text.length(); ++i) { 
     char x = text.charAt(i); 
     if (isVowel(x)) { 
      vowelbuf += x; 
     } else { 
      if (vowelbuf.length() > 0) { 
       ret += vowelbuf + "b" + vowelbuf + x; 
       vowelbuf = ""; 
      } else { 
       ret += x; 
      } 
     } 
    } 
    if (vowelbuf.length() > 0) { 
     ret += vowelbuf + "b" + vowelbuf; 
    } 
    return ret; 
} 
+0

非常感謝你! – Nando

相關問題