统计信息

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

Mock test

2008-04-21 14:43:51

天气: 晴朗 心情: 高兴

/*
 *Name:Wang Chaoran
 *Description:
 * Write a program which accepts an unspecified number of
 * user input sentences and counts the number of occurrences of each letter.
 * The program then outputs the letters with the highest and lowest frequencies.
 * A sample interaction session is as follows:

Enter a sentence:
Good morning!
Continue(Y/N)? y
Enter a sentence:
Life is hard. Why not make it harder.
Continue(Y/N)? Y
Enter a sentence:
The quick brown fox jumps over the lazy dog.
Continue(Y/N)? n

You entered 3 sentences:
Good morning!
Life is hard. Why not make it harder.
The quick brown fox jumps over the lazy dog.

The letter with the most number of occurrences (<x> time(s)) is <char-most>.
The letters with the least number of occurrences (<y> time(s)) are <char-least1>, <char-least2>, <char-least3>.

Note that y > 0. For simplicity you may assume that each sentence is terminated by a carriage return.
You may make other reasonable assumptions but should make these explicit at the start of your program.

You should demonstrate good programming style with appropriate use of comments,
indentation and meaningful identifier names. Top down development with modular
methods are highly encouraged. Justify the robustness of your program with
representative test cases, which purposes you should explain.

Comment on the usefulness of this program (or its extension) with respect to Computing.
*/
import java.util.Scanner;
class Letters{
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    String inputJudge = "y";
    String inputSentence="";
    int count = 0;
    //input loop
  while(((inputJudge.equals("y"))||(inputJudge.equals("Y")))&&((!inputJudge.equals("N"))||(!inputJudge.equals("n")))){
      System.out.println("Enter a sentence:");
      inputSentence+=scan.nextLine()+"\n";
      count++;
      System.out.println("Continue(Y/N)?");
      inputJudge = scan.nextLine();
    while((!inputJudge.equals("y"))&&(!inputJudge.equals("Y"))&&(!inputJudge.equals("N"))&&(!inputJudge.equals("n"))){
      System.out.println("Invalid.Please enter either Y/N!");
      System.out.println("Continue(Y/N)?");
      inputJudge = scan.next();
      }
  }
    //count letters
    int [] letter=new int [26];
    for(int i=0;i<inputSentence.length();i++){
    char current = inputSentence.charAt(i);
    int currentNum = (int)(current);
    if ((currentNum>=(int)('a'))&&(currentNum<=(int)('z')))
      letter[currentNum-(int)('a')]++;
    else if((currentNum>=(int)('A'))&&(currentNum<=(int)('Z')))
      letter[currentNum-(int)('A')]++;
    }
   
    int max=letter[0];
    int indexMax=0;
    for(int i=1;i<letter.length;i++){
      if(letter[i]>max){
      max=letter[i];
      indexMax=i;
      }
    }

    int least=letter[indexMax];
    int indexLeast=indexMax;
    for(int i=1;i<letter.length;i++){
     if((letter[i]<least)&&(letter[i]!=0)){
      least=letter[i];
        indexLeast=i;
      }
    }
    //Output.
    System.out.println("You entered "+count+" sentences:");
    System.out.println(inputSentence);
    System.out.print("The letter with the most number of occurrences (<"+max+"> time(s)) is ");
    for(int i=0;i<letter.length;i++){
      if(letter[i]==max)
    System.out.print(("<"+(char)((int)('a')+i))+"> ");
    }
    System.out.print(".\n");
    System.out.print("The letters with the least number of occurrences (<"+least+"> time(s)) is ");
    for(int i=0;i<letter.length;i++){
    if(letter[i]==least)
    System.out.print(("<"+(char)((int)('a')+i))+"> ");
    }
    System.out.print(".");

}

}

 


加入收藏 编辑 审核

TAG: computing

我来说两句

OPEN

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