使用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') ) /* 来 自*