lettercounting.java

来自「Java 程序设计源码 只提供了部分」· Java 代码 · 共 48 行

JAVA
48
字号

import javax.swing.*;

public class LetterCounting {
	public static final int LETTERNUMBER = 26;
	
	public static void main(String[] args) {
		String s = JOptionPane.showInputDialog(null,"输入一个字符串:","实例输入",JOptionPane.QUESTION_MESSAGE);

	//invoking the count letters method to count each letter.
	int[] numberUpper = countUpper(s);
	int[] numberLower = countLower(s);

	//declare and initialize the output string
	String output = "";
	
	//display the result
	for(int i = 0; i < numberUpper.length; i++)
		if(numberUpper[i] != 0)
			output += (char)('A' + i) + "出现" + numberUpper[i] + "次数\n";	

	for(int i = 0; i < numberLower.length; i++)
		if(numberLower[i] != 0)
			output += (char)('a' + i) + "出现" + numberLower[i] + "次数\n";

	JOptionPane.showInputDialog(null,output,"实例输出",JOptionPane.INFORMATION_MESSAGE);
	}

	public static int[] countUpper(String s) {
		int[] count = new int[LETTERNUMBER];

		for(int i = 0; i < s.length(); i++) {
			if(Character.isUpperCase(s.charAt(i)))
				count[(int)(s.charAt(i) - 'A')]++;
		}
		return count;
	}

	public static int[] countLower(String s) {
		int[] count = new int[LETTERNUMBER];

		for(int i = 0; i < s.length(); i++) {
			if(Character.isLowerCase(s.charAt(i)))
				count[(int)(s.charAt(i) - 'a')]++;
		}
		return count;
	}
}

⌨️ 快捷键说明

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