统计信息

  • 访问数:6142
  • 博客数:141
  • 建立时间:2008-01-05
  • 更新时间:2008-05-09
我叫王超然,是一名电脑爱好者,现在在新加坡留学上高一.我立志成为一名电脑人才,愿意在这里与大家一同分享我玩转电脑的心得.O-level华文考了A-One哈哈!

mock test

2008-04-30 08:46:34

天气: 晴朗 心情: 高兴

/* Filename: CountLetters.java
 */

import java.util.Scanner;

public class CountLetters {
  public static void main(String[] args) {
   
    // Declare constants
    final int MAX_SENTENCES = 100;
    final int MAX_LETTERS = 26;
   
    // Declare variables
    Scanner scan = new Scanner(System.in);
    String[] sentences = new String[MAX_SENTENCES];
    int sCount = 0; // count number of sentences
    char toContinue;
    String clearLine;
   
    // Get sentences from standard input
    do {
      System.out.println("Enter sentence:");
      sentences[sCount++] = scan.nextLine();
      System.out.print("Continue(Y/N)?");
      toContinue = Character.toUpperCase(scan.next().charAt(0));
       clearLine = scan.nextLine();// flush buffer
      while ((toContinue != 'Y') && (toContinue != 'N')) {
        System.out.println("Invalid choice!");
        System.out.print("Continue(Y/N)?");
        toContinue = Character.toUpperCase(scan.next().charAt(0));
        clearLine = scan.nextLine();       
      }
    } while ((toContinue == 'Y') && (sCount <= MAX_SENTENCES));
 
    // Store letter count
    int[] letterCount = new int[MAX_LETTERS];
    char current;
    for (int i = 0; i < sCount; i++) {
      for (int ch = 0; ch < sentences[i].length(); ch++) {
        current = Character.toUpperCase(sentences[i].charAt(ch));
        if ((current >= 'A') && (current <= 'Z'))
          letterCount[current - 'A']++;
      }
    }
   
    // Display count
    for (int i = 0; i < letterCount.length; i++)
      System.out.println("letter count " + (char)(i+'A') + ":" + letterCount[i]);
   
    // Process max & min
    int max = letterCount[0];
    //int min = ;
    for (int i = 1; i<MAX_LETTERS; i++) {
      if (letterCount[i] > max)
        max = letterCount[i];
    }
   
    // Verify output
    System.out.println("You entered " + sCount + " sentences:");
    for (int j = 0; j < sCount; j++)
      System.out.println(sentences[j]);
  }
}


加入收藏 编辑 审核

TAG: computing

我来说两句

OPEN

Powered by X-Space 1.2 © 2001-2006 Comsenz Technology Ltd