根据随机数创建直方图



当我试图从文件中获取的随机数据创建直方图时,我的程序会添加范围之间的数字,而不是增加星形数字。假设我有3个数字(2,3,7(在1-10的范围内。它不是打印三星,而是打印2、3和7的总和。这里有什么问题?请帮忙!非常感谢。

import javax.imageio.IIOException;
import java.util.*;     // import all the Input/Ouput classes
import java.io.*;       // import all the Utility classes (Scanner, etc.)

public class histogram
{
public static void main( String[] args )
throws IIOException, FileNotFoundException
{
int word, i, j, h1 = 0, h2 = 0, h3 = 0, h4 = 0, h5 = 0,
h6 = 0, h7 = 0, h8 = 0, h9 = 0, h10 = 0,     // integer variable for loop iteration
sum = 0, // sum of all the letters
avg_ch = 0, // average of all the letters in a word
asci ; // ascii value of the chracters

char ch = 0;

Scanner scan = new Scanner(System.in);     // attach to System.in to for console input
System.out.printf("Please enter the input file name: ");     // Display message
String inputfilename = scan.next();    // prompts user for input file name to read.
System.out.printf("Please enter the output file name: ");     // Displays message
String outputfilename = scan.next();    // prompts user for output file name.
//  makes the input and output file names constant.
final String FILENAME_DATA = inputfilename;
final String FILENAME_REPORT = outputfilename;
Scanner scanFile = new Scanner(new File(FILENAME_DATA));    // opens input file
PrintWriter outfile = new PrintWriter( new File(FILENAME_REPORT));    // opens output file.
while (scanFile.hasNext() == true)     // repeat until there is no items left.
{
word = scanFile.nextInt();    // scans the item names from input file 

if (word >0 && word <11)
{
h1++;
}

else if (word >10 && word <21)
{
h2++;
}

else if (word >20 && word <31)
{
h3++;
}

else if (word >30 && word <41)
{
h4++;
}

else if (word >40 && word <51)
{
h5++;
}

else if (word >50 && word <61)
{
h6++;
}

else if (word >60 && word <71)
{
h7++;
}

else if (word >70 && word <81)
{
h8++;
}

else if (word >80 && word <91)
{
h9++;
}

else if (word >90 && word <100)
{
h10++;
}


}
for (i=0; i< h1 ; i++)
{
System.out.printf("*");    // Display message
outfile.printf("*");    // prints message to the output file

}
System.out.println();

for (i=0; i< h2 ; i++)
{
System.out.printf("*");    // Display message
outfile.printf("*");    // prints message to the output file

}
System.out.println();

for (i=0; i< h3 ; i++)
{
System.out.printf("*");    // Display message
outfile.printf("*");    // prints message to the output file

}
System.out.println();

for (i=0; i< h4 ; i++)
{
System.out.printf("*");    // Display message
outfile.printf("*");    // prints message to the output file

}
System.out.println();

for (i=0; i< h5 ; i++)
{
System.out.printf("*");    // Display message
outfile.printf("*");    // prints message to the output file

}
System.out.println();

for (i=0;  i< h6 ; i++)
{
System.out.printf("*");    // Display message
outfile.printf("*");    // prints message to the output file

}
System.out.println();

for (i=0;  i< h7 ; i++)
{
System.out.printf("*");    // Display message
outfile.printf("*");    // prints message to the output file

}
System.out.println();

for (i=0 ; i< h8 ; i++)
{
System.out.printf("*");    // Display message
outfile.printf("*");    // prints message to the output file

}
System.out.println();    
for (i=0 ; i< h9 ; i++)
{
System.out.printf("*");    // Display message
outfile.printf("*");    // prints message to the output file

}
System.out.println();

for (i=0; i< h10 ; i++)
{
System.out.printf("*");    // Display message
outfile.printf("*");    // prints message to the output file

}
System.out.println();

outfile.close();    // closes th eoutput file
scanFile.close();   // closes the input file
}
}

问题可能是在直方图条之间向System.out打印换行符,而不是打印到输出文件。这将生成一个输出文件,其中所有条形图都堆叠在一行上。

其他随机提示:

你可以简化你的if-else堆栈:

if (word <= 0)
{
// do not increment any counters
}
else if (word < 11) // can't be less than or equal to 0 because that was already handled above
{
h1++;
}
else if (word < 21) // can't be less than 11 because that was already handled above
{
h2++;
}
else if (word < 31) // can't be less than 21 because that was already handled above
{
h3++;
}

... etc

此外,java类的标准是以大写字母开头。

最新更新