Java 使用星号的水平条形图
我正在尝试编写一个字母频率程序,该程序对.txt文件中的字母字符进行计数,并在3列表格(字母、频率、*条形图)中显示频率数据。我目前有代码打印出所有正确的除了条形图。我不知道如何打印星号条形图。任何指导都将不胜感激。谢谢Java 使用星号的水平条形图,java,Java,我正在尝试编写一个字母频率程序,该程序对.txt文件中的字母字符进行计数,并在3列表格(字母、频率、*条形图)中显示频率数据。我目前有代码打印出所有正确的除了条形图。我不知道如何打印星号条形图。任何指导都将不胜感激。谢谢 Letter Count Bar ------ ----- --- A 4 **** B 8 ******** and so on... 这是我的密码: final static int AlphabetSize = 26; final s
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
final static int alphabetize=26;
最终静态扫描仪cin=新扫描仪(System.in);
最终静态打印流cout=System.out;
最终静态int MaxBarLength=50;
公共静态void main(字符串[]args){
字符串文件名;
//获取文件名
cout.print(“输入文件名:”);
fileName=cin.nextLine();
//处理文件
试一试{
进程文件(文件名);
}
捕获(IOE异常){
e、 printStackTrace();
}//结束尝试
}//末端总管
静态void processFile(最终字符串文件名)
抛出FileNotFoundException,IOException
{
FileInputStream inFile=新的FileInputStream(文件名);
int输入值;
//声明您需要的其他变量
int[]计数器=新的int[26];
intα=0;
int num=0;
//从文件中获取第一个字符
inputValue=infle.read();
while(输入值!=-1){
char ch=(char)inputValue;
//添加代码以处理此字符
int c=char2int(ch);
如果(c>=0){
计数器[c]++;
alpha++;
}否则{
num++;
}
//读取下一个输入字符
inputValue=infle.read();
}//结束循环
infle.close();
//生成适当的输出
System.out.println(“\n数据文件有“+alpha+”字母和“+num+”其他字符。\n”);
显示器(计数器);
}//结束函数
静态无效显示(最终int[]计数器){
//为这个函数编写代码
System.out.println(“字母“+”+“计数”+“+”条”);
System.out.println(“----”+“+”----”+“+”----”;
打印字符(计数器);
}//结束函数
//char2int已完成
静态int char2int(最终字符参数){
if(!Character.isleter(arg))
返回-1;
其他的
返回(int)字符。toUpperCase(arg)-(int)“A”;
}//结束函数
//函数printChars将字符c的n个副本写入
//标准输出设备
静态无效打印字符(最终int[]计数器){
//编写代码
对于(int i=0;i<26;i++){
System.out.printf(“%c%7d\n”,i+'A',计数器[i]);
}
}//结束打印字符
如果要在每次字母出现时打印一个“*”:
// 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();
}
}
//函数printChars将字符c的n个副本写入
//标准输出设备
静态无效打印字符(最终int[]计数器){
//编写代码
对于(int i=0;i<26;i++){
系统输出打印(“%c%7d\n”,i+“A”,计数器[i]);
对于(int j=0;j<计数器[i];j++){
系统输出打印(“*”);
}
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();
}
}
//函数printChars将字符c的n个副本写入
//标准输出设备
静态无效打印字符(最终int[]计数器){
浮动最大杆长=20.0;
int maxScore=0;
对于(int i=0;i<26;i++){
if(计数器[i]>maxScore){
maxScore=计数器[i];
}
}
浮动乘数=maxScore>0?最大条形长度/maxScore:0;
//编写代码
对于(int i=0;i<26;i++){
系统输出打印(“%c%7d\n”,i+“A”,计数器[i]);
对于(int j=0;j<(乘法器*计数器[i]);j++){
系统输出打印(“*”);
}
System.out.println();
}
}
对于我想要的格式,我在@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
静态无效打印字符(最终int[]计数器){
//编写代码
对于(int i=0;i<26;i++){
System.out.printf(“%c%7d”,i+'A',计数器[i]);
对于(int bar=0;bar<计数器[i];bar++){
系统输出打印(“*”);
}
System.out.println();
}
}//结束打印字符
请显示运行程序的预期结果。编辑我的帖子以显示示例输出@杰森