Java条形图方法 [英] Java bar chart method

查看:156
本文介绍了Java条形图方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要打印一个条形图,通过从 barChartPrinter 方法的调用。也就是说 barChartPrinter(5,6,2); 将打印:垂直列为5 XX,后跟
a,然后垂直列为6 XX,垂直列2 XX's。

I need to print out a bar chart, via a call from a method barChartPrinter. I.e. barChartPrinter(5, 6, 2); would print: a vertical column of 5 XX's followed by a space then a vertical column of 6 XX's and a vertical column of 2 XX's.

这些数字是基于用户输入的,所以我想我需要收集数字并将它们存储在数组中。我想一个for循环将涉及,
,但我不知道我该怎么做。

The numbers are based on user input, so I'm thinking I need to gather the numbers and store those in an array. And I'm thinking a for loop will be involved, but I'm not sure how I'd do it.

public class BarChart {

    public static void main(String args[]) {
        int[] list = new int[4];
        Scanner input = new Scanner(System.in);
        System.out.println("Enter four integers: ");
        for(int i = 0; i < list.length; i++)
        list[i] = input.nextInt();

        System.out.println();

        barChartPrinter(list);
    }       

    public static void barChartPrinter(int[] numbers) {
        int max = numbers[0];
        for (int i = 0; i < numbers.length; i++)
        if(numbers[i] > max)
            max = numbers[i];
    }
}

这是我遇到的问题:定义方法将打印出的条形图;给定用户输入值。该方法需要以此格式打印,即

This is where I get stuck: defining the method that will print out the bar chart; given the user input values. The method needs to print out in this format, i.e.

barChartPrinter(1, 2, 3, 4) =

由于某些原因,它不会显示如何在这里构造条形图,所以我只是描述:

For some reason, it won't display how the bar chart should be constructed on here so I'll just describe it:

首先是XX列,然后是XX列(其中两列垂直表示2),另一列是XX(但这次是三列这些在顶部,代表数字3),最后另一列XX * 4垂直。

There would first be a column of XX, then a column of XX (two of these vertically to represent 2), then another column of XX (but this time three of these on top of each other, to represent the number 3) and finally another column of XX * 4 vertically.

任何指针?

推荐答案

确定,现在我确信你知道创建水平条形图将非常容易: p>

OK, by now I'm sure that you know that it would be trivially easy to create horizontal bar graphs:

2 6 4

**
******
****

由于控制台的PrintStream的print和println方法( System.out 的输出)水平打印。您的作业的窍门是如何打印此图形垂直。为此,你将不得不使用一点逻辑。因为这显然是作业,我不会给你一个解决方案,而是会建议选项。

Since the print and println methods of the console's PrintStream (the out of System.out) prints things horizontally. The trick for your assignment is how to print this graph vertically. And for that you're going to have to use a little logic. Since this is obviously homework, I'm not going to give you a solution but rather will suggest options.


  • 需要使用某种类型的循环来在一行中打印行。

  • 一个可能的解决方案可以通过首先找到最大值保持由数组。如果你这样做,然后使用for循环。

  • 另一个可能的解决方案,你没有找到max,而只是循环直到完成。这里使用while循环。这将是我将使用的选项,我将使用一个布尔变量,称为 keepGoing 来帮助告诉while循环何时保持循环和何时停止。

  • 如果您使用后一个选项,您将在循环中使用一个int计数器来检查您所在的行,并且您将在循环中推进此计数器。

  • 您需要在while循环中嵌套一个for循环才能遍历每个数组项。

  • 您将使用此计数器和数组项(在while循环中的for循环中),查看最终打印的字符串是否应该添加*

  • 在内循环后,您将打印您创建的String。

  • 如果没有* myString.trim()。isEmpty()),然后 keepGoing = false; 并且while循环应该停止。

  • You know that you'll need to use a loop of some sort to print lines in a row.
  • One possible solution has you figuring out how many times to loop in advance of the loop by first finding the maximal value held by the array. If you do this, then use a for loop.
  • Another possible solution you don't find max, but rather just loop til done. Here use a while loop. This would be the option I'd use, and I'd use a boolean variable, say named keepGoing to help tell the while loop when to keep looping and when to stop.
  • If you go with this latter option, you'll use an int counter in the loop to check what row you're on, and you'll advance this counter inside of the loop.
  • You will need to nest a for loop inside of your while loop to go through each array item.
  • You'd use this counter and the array items (in the for loop within the while loop) to see if the String that you will eventually print should have an "* " added to it.
  • After the inner for loop, you'll print the String you've created.
  • If no "* " are present (you could call myString.trim().isEmpty()), then keepGoing = false; and the while loop should stop.

编辑:您发布的代码有点偏离,它告诉用户输入四个数字,但只接受三个。

your posted code is a bit off in that it tells the user to enter four numbers but only accepts three. You'll want to fix this.

编辑2

Edit 2 You state:


嘿,对不起我已经把代码排序了。我已经编码的方法来找到方法中的最大值。我现在坚持在实际生产图表的步骤。我知道它将水平打印XX的每一行,但我仍然困惑,如何我可以实现这 - 我知道它将涉及一个for循环,但我不能看到我如何打印XX的每列;如果我缺少一些明显的东西,请道歉

Hey, sorry yeah I've sorted the code out. I've coded the way to find the max value within the method. I'm stuck on the step of actually producing the chart now. I know that it will print the XX's each row horizontally, but I'm still stumped as to how I can achieve this- I know it will involve a for loop, but I can't see how I can print the XX's for each column; apologies if I'm missing something obvious

再次,通过把大问题分成小步,可以想到,然后解决每个小步骤。你知道你将要构造一个要打印的字符串,所以你应该关注这个,如何构造这个String,以便在需要的地方出现一个星号,并且当该列应该不显示星号。尝试提出一个解决方案,甚至一个部分解决方案,并将其作为一个编辑您的问题,很像我发布这个编辑到我的答案。

Again, break up your problem by splitting up the big problem into little steps, the smallest you can think of, and then solve each small step. You know that you're going to have to construct a String to be printed, and so you should focus on that, on how to construct this String so that an asterisk is present where need be, and a space is present when the column should show no asterisk. Try to come up with a solution, even a partial solution and post it as an edit to your question, much like how I'm posting this edit to my answer.

这篇关于Java条形图方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆