lettercount.java

来自「java源程序 对初学者有很大的帮助 从简单到复杂」· Java 代码 · 共 55 行

JAVA
55
字号
//********************************************************************//  LetterCount.java       Author: Lewis/Loftus////  Demonstrates the relationship between arrays and strings.//********************************************************************import cs1.Keyboard;public class LetterCount{   //-----------------------------------------------------------------   //  Reads a sentence from the user and counts the number of   //  uppercase and lowercase letters contained in it.   //-----------------------------------------------------------------   public static void main (String[] args)   {      final int NUMCHARS = 26;      int[] upper = new int[NUMCHARS];      int[] lower = new int[NUMCHARS];      char current;   // the current character being processed      int other = 0;  // counter for non-alphabetics      System.out.println ("Enter a sentence:");      String line = Keyboard.readString();      //  Count the number of each letter occurence      for (int ch = 0; ch < line.length(); ch++)      {         current = line.charAt(ch);         if (current >= 'A' && current <= 'Z')            upper[current-'A']++;         else            if (current >= 'a' && current <= 'z')               lower[current-'a']++;            else               other++;      }      //  Print the results      System.out.println ();      for (int letter=0; letter < upper.length; letter++)      {         System.out.print ( (char) (letter + 'A') );         System.out.print (": " + upper[letter]);         System.out.print ("\t\t" + (char) (letter + 'a') );         System.out.println (": " + lower[letter]);      }      System.out.println ();      System.out.println ("Non-alphabetic characters: " + other);   }}

⌨️ 快捷键说明

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