简体   繁体   English

将字符串中的元音大写 java

[英]capitalize vowels in a string java

I'm trying to find a way to make an inputted string's vowels become capitalized after counting the vowels within it.我试图找到一种方法,在计算其中的元音后,使输入的字符串的元音变为大写。

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

String is immutable. String是不可变的。 I would use a StringBuilder .我会使用StringBuilder And I would prefer an if to that complicated switch statement.与复杂的switch语句相比,我更喜欢if Also, I'd use formatted output at the end.另外,我会在最后使用格式化输出。 Something like,就像是,

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());
  • Use a StringBuilder to store and modify the characters at the positions you want to.使用StringBuilder在您想要的位置存储和修改字符。
  • Use the Character class to convert a character to upper/lower case使用Character类将字符转换为大写/小写

For example例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM