2015-10-20 131 views
-2

我想找到一種方法來使得輸入字符串的元音在計入元音後變成大寫。在字符串中大寫元音java

import java.util.*; 
public class Main { 

public static void main(String[] args) { 
    //  Vowels test = new Vowels(); //comment out this line 
    System.out.println("enter a string"); //Says enter String 
    Scanner kb = new Scanner(System.in); //Com reads the input 
    String str = kb.nextLine();  // this is the String variable to the input 
    // char vowel = str.charAt(p); 
    int count = 0;   //this counts thee vowels 
    for(int p = 0; p<str.length(); p++)   // p is the looping for the char vowel 
     { 
      char vowel = str.charAt(p); 
                //.Character.toUppercase(); //Character.toUpperCase doesn't work so much here... 
             // char vOwEl = Character.toUpperCase(vowel); //Adding a new char doesnt work either 
      switch (vowel) 
      {/* I forget the single quotes here...*/ 
      case 'a': 
       // a-=32; 
       /*Character.toUpperCase('a'); 
       count++; 
       break;//didn't work...*/ 
      case 'e': 
       // e-=32; 
       /*Character.toUpperCase('e'); 
       count++; 
       break; */ 
      case 'i': 
       //i-=32; 
       /* Character.toUpperCase('i'); 
       count++; 
       break;*/ 
      case 'o': 
       // o-=32; 
       /* Character.toUpperCase('o'); 
       count++; 
       break;*/ 
      case 'u': 
       //u-=32; 
       //Character.toUpperCase('u'); 
       count++; 
                  //System.out.println("There are "+count+"vowels in the string: "+str); 
       break; 
      default: 
                  //System.out.println("There is no vowels."); 
                  // no code since it will print out "there is no vowels" a number of times 
      } 
     } 
     System.out.println("There are "+count+" vowels in the string: "+str); 


    //  test.countVowels(str); //comment out this line 
} 
} 
+2

你的問題本質上是:「「這裏有一些廣泛的需求,這裏是一些代碼」'和就是這樣。這些問題非常難以回答,並且通常會被關閉。請告訴我們更多關於這個代碼的內容,它做了什麼,它沒有做它應該做什麼,請嘗試提出一個更具體和可回答的問題,你可能會得到一個體面的,具體的答案。經驗法則是問題的質量越好,質量越好,答案越快。 –

+0

你有什麼問題? –

+0

你的代碼絕對沒有錯,它計算元音。 – ergonaut

回答

1

String是不可變的。我會使用StringBuilder。我更希望if這個複雜的switch聲明。另外,我會在最後使用格式化輸出。喜歡的東西,

System.out.println("enter a string"); 
Scanner kb = new Scanner(System.in); 
String str = kb.nextLine(); 
int count = 0; 
StringBuilder sb = new StringBuilder(); 
for (char ch : str.toCharArray()) { 
    char t = Character.toUpperCase(ch); 
    if (t == 'A' || t == 'E' || t == 'I' || t == 'O' || t == 'U') { 
     sb.append(t); 
     count++; 
    } else { 
     sb.append(ch); 
    } 
} 
System.out.printf("There are %d vowels in the string: %s%n", count, 
     sb.toString()); 
+0

這可能是個人選擇,但考慮到可能值的範圍有限,「switch」語句可能稍微容易閱讀,然後是「if」語句,但這只是我;) – MadProgrammer

1
  • 使用StringBuilder到你想要的位置存儲和修改的字符。
  • 使用Character類的字符轉換爲大/小寫

例如

System.out.println("enter a string"); //Says enter String 
Scanner kb = new Scanner(System.in); //Com reads the input 
String str = kb.nextLine();  // this is the String variable to the input 
int count = 0;   //this counts thee vowels 
StringBuilder result = new StringBuilder(str.toLowerCase()); 
for (int p = 0; p < str.length(); p++) // p is the looping for the char vowel 
{ 
    char vowel = str.charAt(p); 
    switch (vowel) {/* I forget the single quotes here...*/ 

     case 'a': 
     case 'e': 
     case 'i': 
     case 'o': 
     case 'u': 
      result.setCharAt(p, Character.toUpperCase(vowel)); 
      count++; 
      break; 
    } 
} 

str = result.toString(); 
System.out.println("There are " + count + " vowels in the string: " + str);