java - 如何打印另一个类中的数组和方法

标签 java arrays histogram

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Histogram
{
  public static void main(String[] args) throws FileNotFoundException
  {
    Row[] numbers = {
           new Row("1  -  10"), new Row("11 -  20"), new Row("21 -  30")                 
           new Row("31 -  40"), new Row("41 -  50"), new Row("51 -  60")
           new Row("61 -  70"), new Row("71 -  80"), new Row("81 -  90"),
           new Row("91 - 100")
                };
for(Row number : numbers)
  System.out.print(number);   
Counter section = new Counter();
section.StarCounter();

}

它打印出如下内容:

1  - 10|
11 - 20|
21 - 30|
*****************
*********
***
ect.

我希望它打印出类似的内容:

1  - 10|*****************
11 - 20|*********
21 - 30|***
ect.

提前感谢您的任何指点,我可能只是忽略了显而易见的事情。

计数器类别:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class Counter
{
  public void StarCounter() throws FileNotFoundException
  {
    File file = new File("alotofnumbers.txt");
    Scanner scan = new Scanner(file);
    int[] integers = new int[1000];
    int start = 0;
    int count1 = 0;
    int count2 = 0;
    int count3 = 0;
    int count4 = 0;
    int count5 = 0;
    int count6 = 0;
    int count7 = 0;
    int count8 = 0;
    int count9 = 0;
    int count10 = 0;

    while (scan.hasNextInt())
    {
  integers[start++] = scan.nextInt();
}

for (int input : integers)
{
  if(input <= 10)
  {
    count1++;  
  }
  else if(input > 10 && input <= 20)
  {
    count2++;
  }
  else if(input > 20 && input <= 30)
  {
    count3++;
  }
  else if (input > 30 && input <= 40)
  {
    count4++;
  }
  else if (input > 40 && input <= 50)
  {
    count5++;
  }
  else if (input > 50 && input <= 60)
  {
    count6++;
  }
  else if (input > 60 && input <= 70)
  {
    count7++;
  }
  else if (input > 70 && input <= 80)
  {
    count8++;
  }
  else if (input > 80 && input <= 90)
  {
    count9++;
  }
  else if (input > 90 && input <= 100)
  {
    count10++;
  }
} 
  double counted1 = count1 / 2.7;
  double counted2 = count2 / 2.7;
  double counted3 = count3 / 2.7;
  double counted4 = count4 / 2.7;
  double counted5 = count5 / 2.7;
  double counted6 = count6 / 2.7;
  double counted7 = count7 / 2.7;
  double counted8 = count8 / 2.7;
  double counted9 = count9 / 2.7;
  double counted10 = count10 / 2.7;

  for(int star = 0; star <= counted1  ; star++)
  {
   System.out.print("*");

  }
  System.out.println();


  for(int star = 0; star <= counted2 ; star++)
  {
    System.out.print("*");
  }

   System.out.println();
   for(int star = 0; star <= counted3 ; star++)
   {
    System.out.print("*");
   }

     System.out.println();

   for(int star = 0; star <= counted4 ; star++)
   {
     System.out.print("*");
   }

    System.out.println(); 
    for(int star = 0; star <= counted5 ; star++)
    {
     System.out.print("*");
    }

      System.out.println();

    for(int star = 0; star <= counted6 ; star++)
    {
      System.out.print("*");
    }

     System.out.println();
     for(int star = 0; star <= counted7 ; star++)
     {
      System.out.print("*");
     }

       System.out.println();

     for(int star = 0; star <= counted8 ; star++)
     {
       System.out.print("*");
     }

      System.out.println();
      for(int star = 0; star <= counted9 ; star++)
      {
       System.out.print("*");
      }

        System.out.println();

      for(int star = 0; star <= counted10 ; star++)
      {
        System.out.print("*");
      }

       System.out.println();     


 }
 }

显然我的帖子有太多代码,而且我真的没有更多信息可以提供,所以它希望我随机输入一些内容。 公开课 排 { 私有(private)字符串行;

public Row(String colums)
{
rows = colums ;
}

public String toString()
{
Counter section = new Counter();
return rows + "|" + "\n";
}

public void setRow(String colums)
{
rows = colums;
}

public String getRow()
{
return rows;
}

}

最佳答案

我不太确定你的问题是什么,因为标题似乎与问题没有那么密切相关,所以我假设它是“是否有更好的方法来创建直方图?”。

您不能采用打印所有标题(行)然后打印星星的方法。它们必须同时完成。

由于 Java 中的整数除法会自动向下舍入,因此可以显着减少所有计数代码:

int[] counts = new int[GROUPS];
for (int i: inputs)
    counts[Math.min(GROUPS - 1, (i + 1) / 10)]++;

GROUPS 需要定义为静态类常量。

然后打印计数可以简化为:

for (int i = 0; i < GROUPS; i++) {
    System.out.print(String.format("%2d - %2d]", i * 10 + 1, i * 10 + 10));
    for (int s = 0; s < counts[i]; s++)
        System.out.print("*");
    System.out.println();
}

事实上,这可以使用 Java 8 流进一步简化,但这可能比您目前想要的信息更多。

关于java - 如何打印另一个类中的数组和方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33840407/

相关文章:

java - 分代垃圾收集是如何工作的?

java - 线程 "main"java.lang.NullPointerException 中出现异常(请帮助我..)

javascript - 过滤 Javascript 数组以检查所有嵌套对象中的特定值

arrays - 快速更有效地搜索数组以匹配不同类型的方法

c - 删除二维数组中的重复项

R/ggplot 2 - 使用 Facet_grid 和 geom histogram/errorbar 处理不均匀的组大小

r - 拆分数据以在 R 中并排绘制直方图

java - 用于处理扩展类中频繁逻辑更改的设计模式

java - 如何获取泛型函数体内的实际类型?

python - 如何在直方图 bin 中获取数据