📄 lettercounting.java
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -