我很难根据传递给那些负责打印它们的方法来打印出一些星星。这是我到目前为止所使用的代码。我在那些我不确定该怎么做的领域添加了评论。我试图遵循方法的目的所要求的一切。但我这样做并不成功。
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);
}
}
}
}
输出应该是......
*
********
*********
***************
第一个是1.得到(5/46)* 15
第二个是8.发现者(25/46)* 15
依旧......
如何修复我的代码呢?我认为主要问题在于调用drawBar方法和drawBar方法本身。我不认为其他方法编码不正确。
答案 0 :(得分:0)
为什么不提供一个星级酒吧&#34;具有最大恒星数的常量,并使用该常量的子串来获得所需的数字(myStars = C_STARBAR.substring(0,n)
其中n是星数...当然不能超过最大恒星!
答案 1 :(得分:0)
更改drawBar
方法的签名和代码,如下所示。您将获得预期的输出。
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();
}
}
}