我有一个任务,我有两个类,一个驱动程序类,它使用扫描仪实用程序从键盘读取字符串,然后记录它的字母频率。 (每个字母出现在输入字符串中的次数)。我的程序应该继续输入文本行,直到您连续输入两个返回。然后代码应该打印字母频率,然后是报告,提供最频繁的信件和它的计数(在最常见的信件的情况下,任何最频繁的信件会这样做)。此外,我的代码忽略字母大小写 - 所以大写字母以及小写字母应该被计数。代码编译,但不返回任何
我的驱动类
import java.util.*;
public class LetterDriver{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
String tScan = " ";
while(tScan.length() > 0){
tScan = s.nextLine();
}
}
}
我的实际轮廓类是
public class LetterProfile {
int score[] = new int [26];
public void countChars (String 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 void scoreLine (String lines) { // prints letter + # of appearance
int index = lines.length();
for (int j = 0; j<index; j++) {
score[j]++;
System.out.println(j + score[j]);
}
}
public int largestLength() { // finds most frequent letter
int largest = 0;
int largestindex = 0;
for(int a = 0; a<26; a++)
if(score[a]>largest){
largest = score[a];
largestindex = a;
}
return largestindex;
}
public void printResults() {
largestLength();
System.out.println(largestLength());
}
}
再次我的代码编译,当我运行它让我进入我的文字输入,但是当我回到两次都是我得到空白输出。我认为这可能与我的配置文件类没有正确从我的驱动程序类读取,但无法弄清楚什么是错的。
请问为什么它是letterProfile.countChars(TSCAN);而不是s?是不是我的字符串输入? – aiuna 2013-03-19 17:33:20
's'是'Scanner'对象。使用扫描仪,您正在读取变量'tScan'中的字符串,您将使用该字符串。 – Amar 2013-03-19 17:35:37
好的,谢谢,我觉得司机班很好走,但我的个人档案类时髦,所以它没有返回我所需的输出.. – aiuna 2013-03-19 17:38:16