Java 将垂直直方图打印到控制台
我需要编写一个void方法()“printHistogram(int-bin)”,该方法打印主人关于他们养的狗的直方图。参数是“int-bin”,用于将直方图划分为区间。一个用户最多可以养10只狗。因此,考虑bin=5,在这种情况下的间隔将是2,因为10/5=2。因此,间隔为(0-5,5-10)Java 将垂直直方图打印到控制台,java,console,histogram,Java,Console,Histogram,我需要编写一个void方法()“printHistogram(int-bin)”,该方法打印主人关于他们养的狗的直方图。参数是“int-bin”,用于将直方图划分为区间。一个用户最多可以养10只狗。因此,考虑bin=5,在这种情况下的间隔将是2,因为10/5=2。因此,间隔为(0-5,5-10) _ _ _ _ _ 0-5 5-10 这意味着4个用户的狗数量在0到5之间,以此类推。不需要知道狗计数等方法,我只需要逻辑和算法。我真的需要帮助。谢谢此方法的输出是打印“u”下划线并构造垂直
_
_
_
_ _
0-5 5-10
这意味着4个用户的狗数量在0到5之间,以此类推。不需要知道狗计数等方法,我只需要逻辑和算法。我真的需要帮助。谢谢此方法的输出是打印“u”下划线并构造垂直直方图
这是我的代码:-
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();
}
}
}
public void draw直方图(int-bin){
int highestDogs=owners.get(0.countDogs();//获取用户的狗数的方法
整数间隔=10/箱;
内部温度=0;
int tempBin=bin;
对于(int i=0;i=temp&&owners.get(i).countDogs()bin)
{
系统输出打印(“”);
}
系统输出打印(“”);
temp=tempBin;
tempBin=bin+bin;
System.out.println(tempBin);
}
否则{
temp=tempBin;
tempBin=bin+bin;
System.out.println(tempBin);
}
}而(tempBin<11);
System.out.println();
}
}
}
这是一个绘制直方图的工具。请注意以下评论:
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();
}
}
import java.util.ArrayList;
导入java.util.array;
导入java.util.List;
公共班机{
静态列表所有者;//表示每个所有者的狗数
静态最终整数最大值=10;
公共静态void main(字符串[]args){
//测试数据
owners=Arrays.asList(新整数[]{0,1,2,3,4,5,6});
绘制直方图(2);
}
静态虚空绘制直方图(整箱){
//todo添加检查以确保bin=列GetIntervalStart())
&&(numberOfDogs maxHeight){maxHeight=col.getQty();}
}
maxHeight+=1;//为列页脚添加1
}
//用空格、标记或页脚填充GraphResentation
私人无效声明(){
GraphRespresentation=新字符串[maxHeight][columns.size()];
对于(int colIndex=0;colIndex=0;rowIndex--){
字符串s=空格;
如果(rowCounter==0){//柱状图页脚
s=列GetIntervalStart()+“-”+列getIntervalEnd();
}否则如果(rowCounter看起来是一项具有挑战性的任务。您是否有任何伪代码或首次尝试?经过几天的尝试,您得出了什么结论?我编辑了我的问题。我编辑了我的代码好几次,现在,我甚至不知道我的方向是否正确。我不知道它应该做什么,输入是什么,预期输出是什么。)编辑了这篇文章。