I am having a hard time trying to print out a number of stars based on what is being passed to those methods that are responsible for printing them. Here is the code that I worked on so far. I added comments to those areas I am not sure about what to do. I am trying to follow everything that the Purpose of the methods ask of me. But I have been unsuccessful in doing so.
public class Stars
{
private static final int MAX_STARS = 15;
private static String BAR_REP = "*";
private static DecimalFormat valueFormatter = new DecimalFormat("00.00");
private static DecimalFormat indexFormatter = new DecimalFormat("000");
public static void main (String[] args)
{
processQuestionData("5,25,30,36,46");
}
public static void processQuestionData(String data)
{
double[] values = fillArray(data);
double max = findMax(values);
drawBar(values, max); // This won't work but I have to pass 2 double values.
}
/**
* PURPOSE: accepts a String data containing a comma separated list
* and converts it into an array of double values
*
* @param String data - comma separated string containing double data
*
* @return double [] - array containing the data extracted from data
*/
public static double[] fillArray(String data)
{
String[] answers = data.split(",");
double[] values = new double[answers.length];
for (int index = 0; index < answers.length; index++)
{
values[index] = Double.parseDouble(answers[index]);
}
return values;
}
/**
* PURPOSE: given a an array of double values find the largest item
*
* @param double[] items - an array of double data
*
* @return double the largest item in the array
*/
public static double findMax(double[] items)
{
double maxValue = items[0];
for(int index = 0; index < items.length; index++)
{
if (items[index] > maxValue)
{
maxValue = items[index];
}
}
return maxValue;
}
**
* PURPOSE: given a value and a max as double type arguments,
* draws a bar using '*' representing the value, scaled
* so that max value would equal the constant MAX_STARS
*
* @param double value - the value to be displayed by the bar in the chart
* @param double max - the max value for the bar chart
*/
public static void drawBar(double value, double max )
{
double correctRatio = (value / max) * MAX_STARS;
if (correctRatio == MAX_STARS)
{
for (int index = 0; index < max; index++)
{
System.out.print(BAR_REP);
}
}
else if (correctRatio < MAX_STARS)
{
for (int index = 0; index < max; index++)
{
System.out.print(BAR_REP);
}
}
}
}
Output should be this...
*
********
*********
***************
1st one is 1. Got that by (5 / 46) * 15
2nd one is 8. Found by (25/46) * 15
And so on....
How can I fix my code to exactly that? I believe that the major problem lies in calling the drawBar method and within the drawBar method itself. I don't think that the other methods are improperly coded.
为什么不提供一个具有最大星星数的“星条”常量,并使用该常量的子字符串获取所需的数字( myStars = C_STARBAR.substring(0,n)
,其中n是星星数。当然不占最大星数!
Change the signature and code of drawBar
method like below. You will get your expected output.
public class Stars {
private static final int MAX_STARS = 15;
private static String BAR_REP = "*";
public static void main (String[] args){
processQuestionData("5,25,30,36,46");
}
public static void processQuestionData(String data) {
double[] values = fillArray(data);
double max = findMax(values);
drawBar(values, max);
}
public static double[] fillArray(String data) {
String[] answers = data.split(",");
double[] values = new double[answers.length];
for (int index = 0; index < answers.length; index++) {
values[index] = Double.parseDouble(answers[index]);
}
return values;
}
public static double findMax(double[] items) {
double maxValue = items[0];
for(int index = 0; index < items.length; index++) {
if (items[index] > maxValue) {
maxValue = items[index];
}
}
return maxValue;
}
public static void drawBar(double[] values, double max ) {
for(int count=0;count<values.length;count++) {
double value = values[count];
int correctRatio = (int)((value / max) * MAX_STARS);
for(int stars=1;stars<=correctRatio;stars++){
System.out.print(BAR_REP);
}
System.out.println();
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.