有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将垂直直方图打印到控制台

我需要编写一个void方法()“printHistogram(int-bin)”,它可以打印主人关于他们养的狗的直方图。参数是“int bin”,用于将直方图划分为区间。一个用户最多可以养10条狗。因此,考虑bin=5,在这种情况下的间隔将是2,因为10/5=2。因此,间隔为(0-5,5-10)

_
_
_
_     _
0-5  5-10

这意味着4个用户的狗在0到5之间,以此类推。不需要知道狗的计数等方法,我只需要逻辑和算法。我真的需要帮助。谢谢该方法的输出是打印“”下划线并构建垂直直方图

这是我的代码:-

public void drawHistogram(int bin) {

      int highestDogs = owners.get(0).countDogs(); //method for getting number of dogs of a user
      int intervals = 10/bin;
      int temp = 0;
      int tempBin = bin;

      for (int i =0; i < owners.size(); i++) 
      {
         tempBin = bin;
         temp = 0;
         do{
            System.out.println(i + "iteration" + " " + owners.get(i).countDogs() );
            if(owners.get(i).countDogs() >= temp && owners.get(i).countDogs() < tempBin)
            {

               if( tempBin > bin)
               {

                  System.out.print("    ");  
               }


               System.out.print("_  ");
               temp = tempBin;
               tempBin = bin + bin;
               System.out.println(tempBin);

            }
            else { 
               temp = tempBin;
               tempBin = bin + bin;

               System.out.println(tempBin);
            }

         }while(tempBin < 11);
         System.out.println();
      }

   }
}

共 (1) 个答案

  1. # 1 楼答案

    这里是mcve绘制直方图。请注意以下评论:

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    public class Main {
    
        static List<Integer> owners;  //represents number of dogs per owner
        static final int MAX_DOGS_NUM = 10;
    
        public static void main(String[] args) {
            //test data
            owners = Arrays.asList(new Integer[] {0,1,2,3,4,5,6});
            drawHistogram(2);
        }
    
        static void drawHistogram(int bin) {
    
            //todo add check to make sure bin < MAX_DOGS_NUM
            List<Column> columns = makeColumns(bin);
            calcColumnsData(columns);
            System.out.println(new Histogram(columns));
        }
    
        //construct all columns, set interval ends 
        private static List<Column> makeColumns(int bin) {
    
            List<Column> columns = new ArrayList<>();
    
            for (int i = 0 ; i <= MAX_DOGS_NUM ; i += bin ) {
                int intervalEnd = (i+ bin) > MAX_DOGS_NUM ? MAX_DOGS_NUM : (i+ bin);
                columns.add(new Column(i, intervalEnd));
                i++;
            }
            return columns;
        }
    
        //calculate quantity of each column 
        private static void calcColumnsData(List<Column> columns) {
    
            for(Column col : columns) {
                //count the number of owners who has dogs within interval 
                int ownersCounter = 0;   
                for(int numberOfDogs : owners) {
                    if((numberOfDogs >= col.getInteravalStart())
                            && (numberOfDogs <= col.getIntervalEnd())) { ownersCounter++ ;}
                }
                col.setQty(ownersCounter); //update column quantity 
            }
        }
    }
    
    //represents a single histogram column
    class Column {
        //interval ends, quantity of owners 
        int interavalStart, intervalEnd, qty =0;
    
        Column(int interavalStart, int intervalEnd) {
            this.interavalStart = interavalStart;
            this.intervalEnd = intervalEnd;
        }
    
        int getQty() {  return qty; }
    
        void setQty(int qty) {  this.qty = qty; }
    
        int getInteravalStart() {return interavalStart; }
    
        int getIntervalEnd() {return intervalEnd; }
    
        @Override
        public String toString() {
            return interavalStart +"-" +intervalEnd+": "+ qty;
        }
    }
    
    //represents histogram graph
    class Histogram{
    
        private List<Column> columns; //histogram columns 
        //representation of graph mark and space 
        private static final String MARK = "-", SPACE =" ";
        private static final int COLUMN_WIDTH = 8; 
        private int maxHeight =0; //size of highest histogram 
        //histogram data. each row contains makrs or space. last 
        //row contains footer 
        private String graphRepresentation[][]; 
    
        Histogram(List<Column> columns) {
            this.columns = columns;
            calculateMaxHeight();
            prepareGraphRepresentation();
        }
    
        //find tallest column 
        private void calculateMaxHeight() {
            for(Column col : columns) {
                if(col.getQty() > maxHeight) { maxHeight = col.getQty();}
            }
            maxHeight +=1; //add 1 for column footer
        }
    
        //fill graphRepresentation with spaces, marks or footer 
        private void prepareGraphRepresentation() {
    
            graphRepresentation = new String[maxHeight][columns.size()];
    
            for(int colIndex = 0 ; colIndex < columns.size() ; colIndex ++ ) {
    
                Column col = columns.get(colIndex);
                int rowCounter = 0;
    
                for(int rowIndex = maxHeight -1 ; rowIndex >=0; rowIndex   ) {
    
                    String s = SPACE;
                    if (rowCounter == 0 ) { //histogram footer
                         s = col.getInteravalStart()+"-"+col.getIntervalEnd();
                    }else if(rowCounter <= col.getQty()) {
                        s = MARK;
                    }
                    graphRepresentation[rowIndex][colIndex] = format(s);
                    rowCounter++;
                }
            }
        }
    
        //add spaces to s to make it as wide as column width
        private String format(String s) {
    
            int leftSpaces = (COLUMN_WIDTH - s.length())/2;
            int rightSpaces = COLUMN_WIDTH - s.length() - leftSpaces;
    
            StringBuilder sb = new StringBuilder();
            //add left spaces
            for(int spaces =0 ; spaces <leftSpaces; spaces++) {
                sb.append(SPACE);
            }
            sb.append(s);
            for(int spaces =0 ; spaces <rightSpaces; spaces++) {
                sb.append(SPACE);
            }
    
            return sb.toString();
        }
    
        @Override
        public String toString() {
    
            StringBuilder sb = new StringBuilder();
            for(String[] row : graphRepresentation ) {
                for(String s : row ) {
                    sb.append(s);
                }
                sb.append("\n");
            }
    
            return sb.toString();
        }
    }
    

    输出:

    enter image description here