码迷,mamicode.com
首页 > 编程语言 > 详细

那些年,一起学的Java 7-4

时间:2015-03-13 01:55:24      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:java


/**

 * 7-4

 * 编写一个Exercise类,该类中含有如下几个方法

 * 1> Statistics(object arc[])方法:

 * 统计每个字母(不计大小写)出现的次数相对于字母总数的比率,

 * 打印显示这个比率

 * 2> Sort (object arc[])方法:

 * 对给定的数组进行排序。

 * 通过编程创建一个Exercise类的对象,

 * 从主方法中接收用户输入的一段英文文字,

 * 并将其存入到新创建的对象中。

 * 通过调用对象方法显示输出结果。

 */

import java.util.Arrays;
import java.util.Scanner;
 
 
public class Test{
	public static void main(String[] args){
		Scanner read = new Scanner(System.in);
		System.out.println("输入一段英文文字");
//		char[] text = new char[100];
//		int i = 0;
//		while (read.nextInt() != -1)
//			text[i++] = (char) read.nextInt();
		String text = read.next();
		Exercise exercise = new Exercise();
		exercise.Statistics(text);
		exercise.mySort(text);
	}
}

class Exercise{
	void Statistics(String text)
	{
		char word[] = new char[256];
		for (int i = 0; i < 256; i++)
			word[i]=0;
		for (int i = 0; i < text.length(); i++)
		{
			int number = text.charAt(i);
			if (number >= 65 && number <= 90)
				number += 32;
			++word[number];
		}
		for (int i = 0; i < 256; i++)
			if (word[i] > 0)
				System.out.println((char)i + " 所占比率为 " + (double)word[i]/text.length());
	}
	void mySort (String text)
	{
		char[] texts = new char[text.length()];
		for (int i = 0; i < texts.length; i++)
			texts[i] = text.charAt(i);
		Arrays.sort(texts);
		for (int i = 0; i < texts.length; i++)
			System.out.print(texts[i] + " ");

	}
}


本文出自 “hacker” 博客,请务必保留此出处http://anglecode.blog.51cto.com/5628271/1619868

那些年,一起学的Java 7-4

标签:java

原文地址:http://anglecode.blog.51cto.com/5628271/1619868

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!