使直方图和星形输出不正确

时间:2013-11-17 05:46:24

标签: java arrays

我正在尝试制作直方图,用户输入的数字为1 - 100,然后程序会看到1-10,11-20等数字的数量。

目前,如果输入是1 2 3 4 5 6 7 8,那么它将在8-星列表中排名81-90,所有这些都有8星。显然,这不是我想要的,但我对此感到难过。

以下是代码:

import java.util.*;

public class test5
{
public static void main (String [] args)
{
  int i = 0;
  int[] Array = new int[10];

  List<Integer> list = new ArrayList<Integer>();

  Scanner s = new Scanner(System.in);

  System.out.println ("Please enter any number of values (from 1 - 100 only) ending with -1 when done:");
  while(i != -1){            
     i = s.nextInt();
     if(i != -1) 
        list.add(i);
  }           

  for (int a = 0; a < list.size(); a++){
     int range1 = 1;
     int range2 = 10;
     for (int b = 0; b < list.size(); b++){
        if (list.get(b) >= range1 && list.get(b) <= range2)
           Array[a] += 1;
     }  
     range1 += 10;
     range2 += 10;  
  }

  for (int count = 0; count < 10; count++){
     System.out.print("| " + (count*10+1) + (count == 0 ? " ": "") + " - " + (count*10+10) + (count == 9 ? "" : " ")
     + "| \t");

     for(int h = 0; h < Array[count]; h++)  
        System.out.print("*");  

     System.out.println();  
  } 
}
} 

4 个答案:

答案 0 :(得分:1)

更改你的第一个for循环。

在您的第一个for循环中, range1 range2 不会更改。由于它们被重新去除,并且在进行修改后未使用range1和range2:range1 += 10; range2 += 10;

在您的代码Array(int [])中,它存储范围数据的数量,例如,

Array [0]存储范围|中的值的数量1 - 10 |

Array [1]存储范围|中的值的数量11 - 20 |

......

Array [9]存储范围|中的值的数量90 - 100 |

我们可以使用一种简单的方法来增加指定数据范围内的值的数量。

以数字11为例,我们可以使用数组[11/10] + = 1,这等于数组[1] + = 1;

确保range1和range2正确无误,或者按照我在下面提到的解决方案进行操作:

更改

for (int a = 0; a < list.size(); a++){
     int range1 = 1;
     int range2 = 10;
     for (int b = 0; b < list.size(); b++){
       if (list.get(b) >= range1 && list.get(b) <= range2)
           Array[a] += 1;
     }  
     range1 += 10;
     range2 += 10;  
 }

 for (int a = 0; a < list.size(); a++){

  if(list.get(a) ==100)
  {
      Array[9] +=1; 
  }else
  {
      Array[list.get(a)/10] +=1; 
      System.out.println(Arrays.toString(Array));
  }

  }

通过此更改,您可以获得预期的结果:

,例如,

INOUT:

12
13
25
36
36
-1

控制台输出:

 | 1  - 10 |
 | 11 - 20 |    **
 | 21 - 30 |    *
 | 31 - 40 |    **
 | 41 - 50 |    
 | 51 - 60 |    
 | 61 - 70 |    
 | 71 - 80 |    
 | 81 - 90 |    
 | 91 - 100| 

答案 1 :(得分:1)

试试这个......

for (int count = 0; count < 10; count++) {
        int start = count * 10 + 1;
        int end = count * 10 + 10;
        System.out.print("| " + (count * 10 + 1) + (count == 0 ? " " : "") + " - " + (count * 10 + 10) + (count == 9 ? "" : " ") + "| \t");

        for (int h = 0; h < list.size(); h++)
            if (list.get(h) >= start && list.get(h) <= end)
                System.out.print("*");
        System.out.println();
    }

答案 2 :(得分:1)

import java.util.*;

public class Test5
{
    public static void main (String [] args)
    {
        int i = 0;
        int[] nums = new int[10];

        List<Integer> list = new ArrayList<Integer>();

        Scanner s = new Scanner(System.in);

        System.out.println ("Please enter any number of values (from 1 - 100 only) ending with -1 when done:");
        while(i != -1){
           i = s.nextInt();
           if(i != -1)
              list.add(i);
        }

        for (int a = 0; a < list.size(); a++){
            nums[(list.get(a)-1) / 10]++;
        }

        for (int count = 0; count < 10; count++){
           System.out.print("| " + (count*10+1) + (count == 0 ? " ": "") + " - " + (count*10+10) + (count == 9 ? "" : " ") + "| \t");

           for(int h = 0; h < nums[count]; h++)
              System.out.print("*");

           System.out.println();
        }
    }
}

测试结果:

$ java Test5
Please enter any number of values (from 1 - 100 only) ending with -1 when done:
1
2
3
44
66
12
13
100
16
-1
| 1  - 10 |     ***
| 11 - 20 |     ***
| 21 - 30 |
| 31 - 40 |
| 41 - 50 |     *
| 51 - 60 |
| 61 - 70 |     *
| 71 - 80 |
| 81 - 90 |
| 91 - 100|     *

答案 3 :(得分:1)

这是一个现场演示,允许用户根据数字分布指定数字并显示十个直方图条:http://dew.apidesign.org/dew/#7510833

方法 bars 执行直方图逻辑。