用Java创建垂直直方图(无数组)

时间:2014-11-20 02:56:40

标签: java histogram

程序创建水平直方图。如何将其更改为垂直直方图,顶部列出类别,每个类别下面都有星号。这是踢球者 - 不使用任何数组(只有循环和if语句)。

    System.out.print("0-29: ");
    for(int v = 0; v < countertwo; v++) {
        System.out.print("*"); }

    System.out.println(" ");
    System.out.println(" ");

    System.out.print("30-39: ");
    for(int v = 0; v < counterthree; v++) {
        System.out.print("*");
    }
    System.out.println(" ");
    System.out.println(" ");

    System.out.print("40-69: ");
    for(int v = 0; v < counterfour; v++) {
        System.out.print("*");
    }
    System.out.println(" ");
    System.out.println(" ");

    System.out.print("70-100: ");
    for(int v = 0; v < counterfive; v++) {
        System.out.print("*");
    }

2 个答案:

答案 0 :(得分:0)

通过查找最大值

计算直方图的高度
max = countertwo;
if(counterthree>max)
max = counterthree;
if(counterfour>max)
max = counterfour;
ect

替代地

max=Math.max(countetOne, Math.max(countertwo, Math.max(counterthree, Math.max.. ect

显示像这样的直方图

for(int i = max; i > 0; i--) //for each row
{

  System.out.print("     "); // spread out the bars

  if(counterone >= i) //if this bar is this tall
     System.out.print("*");
  else
     System.out.print(" ");

  System.out.print("     "); // spread out the bars

  if(countertwo >= i) //if this bar is this tall
     System.out.print("*");
  else
     System.out.print(" ");

  //Do this for each bar


     System.out.println();
 }

 System.out.println("  X Axis label") //print what labels bars
 //May have to adjust spacing between bars depending on length of this

答案 1 :(得分:0)

我想到要显示一个垂直直方图(向下)而不使用数组,你可以通过使用16 if语句来做到如下(16个语句,因为16个可能的结果有4个变量):

    System.out.println("0-29  30-39  40-69  70-100");      

    for(int v = 0; v<countertwo||v<counterthree||v<counterfour||v<counterfive;) {

        if (countertwo>0 && counterthree>0 && counterfour>0 &&counterfive>0) {
            System.out.println(" *      *      *      *");
            countertwo--; counterthree--; counterfour--; counterfive--;
        }
        if (countertwo>0 && counterthree>0 && counterfour>0 &&counterfive==0) {
            System.out.println(" *      *      *");
            countertwo--; counterthree--; counterfour--;
        }
        if (countertwo>0 && counterthree>0 && counterfour==0 &&counterfive>0) {
            System.out.println(" *      *             *");
            countertwo--; counterthree--; counterfive--;
        }
        if (countertwo>0 && counterthree>0 && counterfour==0 &&counterfive==0) {
            System.out.println(" *      *              ");
            countertwo--; counterthree--;
        }
        if (countertwo>0 && counterthree==0 && counterfour>0 &&counterfive>0) {
            System.out.println(" *             *      *");
            countertwo--; counterfour--; counterfive--;
        }
        if (countertwo>0 && counterthree==0 && counterfour>0 &&counterfive==0) {
            System.out.println(" *             *       ");
            countertwo--; counterfour--;
        }
        if (countertwo>0 && counterthree==0 && counterfour==0 &&counterfive>0) {
            System.out.println(" *                    *");
            countertwo--; counterfive--;
        }
        if (countertwo>0 && counterthree==0 && counterfour==0 &&counterfive==0) {
            System.out.println(" *                     ");
            countertwo--;
        }
        if (countertwo==0 && counterthree>0 && counterfour>0 &&counterfive>0) {
            System.out.println("        *      *      *");
            counterthree--; counterfour--; counterfive--;
        }
        if (countertwo==0 && counterthree>0 && counterfour>0 &&counterfive==0) {
            System.out.println("        *      *       ");
            counterthree--; counterfour--;
        }
        if (countertwo==0 && counterthree>0 && counterfour==0 &&counterfive>0) {
            System.out.println("        *             *");
            counterthree--; counterfive--;
        }
        if (countertwo==0 && counterthree>0 && counterfour==0 &&counterfive==0) {
            System.out.println("        *              ");
            counterthree--;
        }
        if (countertwo==0 && counterthree==0 && counterfour>0 &&counterfive>0) {
            System.out.println("               *      *");
            counterfour--; counterfive--;
        }
        if (countertwo==0 && counterthree==0 && counterfour>0 &&counterfive==0) {
            System.out.println("               *       ");
            counterfour--;
        }
        if (countertwo==0 && counterthree==0 && counterfour==0 &&counterfive>0) {
            System.out.println("                      *");
            counterfive--;
        }
        if (countertwo==0 && counterthree==0 && counterfour==0 &&counterfive==0) {
            System.out.println("                       ");   
        }
    }