⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringcharacters.java

📁 Java2入门经典第四章源码
💻 JAVA
字号:
public class StringCharacters
{
  public static void main(String[] args)
  {
    // Text string to be analyzed
    String text = "To be or not to be, that is the question;"
                 +"Whether 'tis nobler in the mind to suffer"
                 +" the slings and arrows of outrageous fortune,"
                 +" or to take arms against a sea of troubles,"
                 +" and by opposing end them?";

    int spaces  = 0,                                 // Count of spaces
        vowels  = 0,                                 // Count of vowels
        letters = 0;                                 // Count of letters

    // Analyze all the characters in the string
    for(int i = 0; i < text.length(); i++)
    {
      // Check for vowels
      char ch = Character.toLowerCase(text.charAt(i));
      if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
        ++vowels;

      //Check for letters
      if(Character.isLetter(ch))
        ++letters;

      // Check for spaces
      if(Character.isWhitespace(ch))
        ++spaces;
    }

    System.out.println
             ("The text contained vowels:     " + vowels + "\n" + 
              "                   consonants: " + (letters-vowels) + "\n"+
              "                   spaces:     " + spaces);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -