集册 Java实例教程 从字符串中提取字符

从字符串中提取字符

欢马劈雪     最近更新时间:2020-01-02 10:19:05

401
使用charAt方法计算用户输入的字符串中的元音数量:

import java.util.Scanner;
/*from 时 代 J a v a 公 众 号 - nowjava.com*/

public class CountVowels

{

     static Scanner sc = new Scanner(System.in);


     public static void main(String[] args){

           System.out.print("Enter a string: ");

           String s = sc.nextLine();


           int vowelCount = 0;


           for (int i = 0; i < s.length(); i++){

                char c = s.charAt(i);

                if ((c == 'A') || (c == 'a')

                       || (c == 'E') || (c == 'e')

                       || (c == 'I') || (c == 'i')

                       || (c == 'O') || (c == 'o')

                       || (c == 'U') || (c == 'u') )
                       /*
                       来 自*
                  
展开阅读全文