我正在使用java扫描仪从键盘输入文本字符串,然后让程序创建一个表,输出az并显示输入字符串中找到的每个字母的编号。 (字母频率)将26个整数的数组转换为字符(字母)
我的司机/主类
import java.util.*;
public class LetterDriver{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
LetterProfile profile = new LetterProfile();
String tScan = " ";
int numReturns = 0;
while(numReturns < 2){
tScan = s.nextLine();
if (tScan.length() == 0){
numReturns++;
}
else{
profile.countChars(tScan);
numReturns = 0;
}
}
profile.printResults();
}
}
我的个人资料类
public class LetterProfile {
int score[] = new int [26];
public void countChars (String s) {
s = s.toLowerCase();
char a = 'a';
for (int i = 0; i < s.length(); i++) {
int next = (int)s.charAt(i) - (int) a;
if (next<=26 && next >= 0)
score[next]++;
}
}
public int largestLength() {
int largest = 0;
int largestindex = 0;
for(int a = 0; a<26; a++) {
if(score[a] > largest) {
largest = score[a];
largestindex = a;
}
}
return (char)largestindex;
}
public void printResults() {
largestLength();
System.out.println(("Most Frequent") + (" :") + largestLength());
}
}
程序编译,当我运行它,它让我可以把我的输入,但只能输出它给了我最常用的数字形式的字母(因为我的数组是0-25,0 = a,1 = b)
基本上我在尝试做的麻烦是让我的代码输出在在显示旁边的字母频率的同时可以显示a-z。有没有简单的方法来完成这件事?
谢谢。
我很难找出如何去做这件事的方法 – aiuna 2013-03-20 00:09:00
啊哎呀。用循环更新 - 我的不好。 – jefflunt 2013-03-20 00:10:05
哇,最后有点进展我现在遇到的唯一问题是在旁边显示字母而不是数字。非常感谢。 – aiuna 2013-03-20 00:16:55