我正在尝试对字母频率程序进行编码,该程序对.txt文件中的字母字符进行计数,并将频率数据显示在3列表格中(字母,频率,*条形图)。我目前除了条形图之外还有正确打印所有内容的代码。我不知道如何打印出星号条形图。任何指导将不胜感激。感谢。
Letter Count Bar
------ ----- ---
A 4 ****
B 8 ********
and so on...
这是我的代码:
final static int AlphabetSize = 26;
final static Scanner cin = new Scanner(System.in);
final static PrintStream cout = System.out;
final static int MaxBarLength = 50;
public static void main(String[] args) {
String fileName;
// get the file name
cout.print("Enter the file name: ");
fileName = cin.nextLine();
// process the file
try {
processFile(fileName);
}
catch (IOException e) {
e.printStackTrace();
} // end try
} // end main
static void processFile(final String fileName)
throws FileNotFoundException, IOException
{
FileInputStream inFile = new FileInputStream(fileName);
int inputValue;
// declare other variables you need
int [] counters = new int [26];
int alpha = 0;
int num = 0;
// get the first character from file
inputValue = inFile.read();
while (inputValue != -1) {
char ch = (char) inputValue;
// add code to process this character
int c = char2int(ch);
if (c >= 0) {
counters[c]++;
alpha++;
} else {
num++;
}
// read next input character
inputValue = inFile.read();
} // end loop
inFile.close();
// generate appropriate output
System.out.println("\nThe data file has " + alpha + " alphabetic, and " + num + " other characters.\n");
display(counters);
} // end function
static void display(final int [] counters) {
// write code for this function
System.out.println("Letter" + " " + "Count" + " " + "Bar");
System.out.println("------" + " " + "-----" + " " + "---");
printChars(counters);
} // end function
// char2int is complete
static int char2int(final char arg) {
if (!Character.isLetter(arg))
return -1;
else
return (int) Character.toUpperCase(arg) - (int) 'A';
} // end function
// function printChars writes n copies of the character c to the
// standard output device
static void printChars (final int [] counters) {
// write the code
for (int i = 0; i < 26; i++){
System.out.printf("%c%7d\n", i + 'A', counters[i]);
}
} // end printChars
答案 0 :(得分:2)
如果你想打印一个&#39; *&#39;每次出现一封信:
// function printChars writes n copies of the character c to the
// standard output device
static void printChars (final int [] counters) {
// write the code
for (int i = 0; i < 26; i++){
System.out.print("%c%7d\n", i + 'A', counters[i]);
for(int j = 0; j < counters[i]; j++) {
System.out.print("*");
}
System.out.println();
}
}
如果你只想表达一个“亲戚”。条形图,您需要将分数标准化。首先计算乘数以对分数进行标准化,然后应用:
// function printChars writes n copies of the character c to the
// standard output device
static void printChars (final int [] counters) {
float MAX_BAR_LENGTH = 20.0;
int maxScore = 0;
for (int i = 0; i < 26; i++){
if(counters[i] > maxScore) {
maxScore = counters[i];
}
}
float multiplier = maxScore > 0 ? MAX_BAR_LENGTH / maxScore : 0;
// write the code
for (int i = 0; i < 26; i++){
System.out.print("%c%7d\n", i + 'A', counters[i]);
for(int j = 0; j < (multiplier * counters[i]); j++) {
System.out.print("*");
}
System.out.println();
}
}
答案 1 :(得分:1)
对于我正在寻找的格式,我在@Jason的代码中编辑了一些内容。
以下是修订后的工作代码:
static void printChars (final int [] counters) {
// write the code
for (int i = 0; i < 26; i++){
System.out.printf("%c %7d ", i + 'A', counters[i]);
for(int bar = 0; bar < counters[i]; bar++){
System.out.print("*");
}
System.out.println();
}
} // end printChars