使用数组的直方图在最终直方图中不计算 int 10
Histogram using arrays not counting int 10 in final histogram
所以我基本上想有一个程序,我输入一组由 space、
分隔的数字
10 20 30 40 50 60 70 80 90 11 22 33 44 55 66 77 88 99 1 31 31 45 98 99 100 500,
当我超过 100 时,程序会根据星号所在的十位范围(1-10、11-20 等)绘制星号
但是,int 10 没有绘制在我的直方图上。我玩弄和修补了小于或等于、以 int 开头的不同索引等等,但我认为问题的根源在于我的 getData(int[] someArray)
方法,但我不确定是什么。
代码:
public class DistributionChart1 {
public static void main(String[] args) {
int size = 10;
int[] ranges = new int[size]; // each entry represents a range of values
getData(ranges); // pass the entire array into the method
displayChart(ranges);
System.out.println("\nSee you later!!");
} // end of main
public static void getData(int[] someArray) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter a series of numbers between 1 and 100. Separate each number with a space.");
System.out.println("Signal the end by entering a number outside " + "of that range and then press enter.");
System.out.print("Go: ");
// reads an arbitrary number of integers that are in the range 1 to 100
int n;
while (scan.hasNext()) {
n = scan.nextInt();
if (n > 100) {
break;
}
int index = n / 10;
if (index == 10) {
index -= index;
break;
}
// for each integer read in, determine which range it is in and increment the
// corresponding element in the array
someArray[index] += 1;
}
scan.close();
}// end of getData
public static void displayChart(int[] someArray) {
// Print chart title with your name
System.out.println("\nDistribution Chart By simonshampoo" + "\n===================================");
// Print histogram.
for (int i = 0; i < 10; i++) {
int beginning = i * 10 + 1;
int ending = beginning + 9;
System.out.print(beginning + "-" + ending + "\t|");
for (int j = 0; j < someArray[i]; j++) {
System.out.print("*");
}
System.out.println();
}
} //
输出:
Enter a series of numbers between 1 and 100. Separate each number with a space.
Signal the end by entering a number outside of that range and then press enter.
Go: 10 20 30 40 50 60 70 80 90 11 22 33 44 55 66 77 88 99 1 31 31 45 98 99 100 500
Distribution Chart By simonshampoo
===================================
1-10 |*
11-20 |**
21-30 |**
31-40 |****
41-50 |***
51-60 |**
61-70 |**
71-80 |**
81-90 |**
91-100 |****
See you later!!
我有 10 和 1,都在 1-10 范围内,但是我从集合中删除了 1,没有星号出现,所以肯定是 10。非常感谢
我很确定不建议删除此处的问题,但我已经弄明白了。这是因为我在 if ( index == 10)
.
中放了一个 break
你的问题是这一行:
int index = n / 10;
对于n = 1-9,计算index = 0
,即该值应计入第1个桶,即桶0。
对于n = 10-19,计算index = 1
,即该值应计入第2个桶,即桶1。
如您所见,n = 10 被计入了错误的桶中。 20、30、40、... 90 也是如此,这就是为什么您需要忽略带有额外 if
(index == 10)
的 100,即使它也应该算在内。
自己看看:
- 尝试输入
9 999
并且 1-10
有 *
- 尝试输入
10 999
并且 11-20
有 *
(糟糕)。
- 尝试输入
100 999
并且不会有星星(哎呀)。
您的代码应该是:
while (scan.hasNext()) {
int n = scan.nextInt();
if (n < 1 || n > 100) {
break;
}
int index = (n - 1) / 10;
someArray[index]++;
}
使用代码,您可以按照说明输入 0
结束输入,但在问题代码中不起作用。
所以我基本上想有一个程序,我输入一组由 space、
分隔的数字10 20 30 40 50 60 70 80 90 11 22 33 44 55 66 77 88 99 1 31 31 45 98 99 100 500,
当我超过 100 时,程序会根据星号所在的十位范围(1-10、11-20 等)绘制星号
但是,int 10 没有绘制在我的直方图上。我玩弄和修补了小于或等于、以 int 开头的不同索引等等,但我认为问题的根源在于我的 getData(int[] someArray)
方法,但我不确定是什么。
代码:
public class DistributionChart1 {
public static void main(String[] args) {
int size = 10;
int[] ranges = new int[size]; // each entry represents a range of values
getData(ranges); // pass the entire array into the method
displayChart(ranges);
System.out.println("\nSee you later!!");
} // end of main
public static void getData(int[] someArray) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter a series of numbers between 1 and 100. Separate each number with a space.");
System.out.println("Signal the end by entering a number outside " + "of that range and then press enter.");
System.out.print("Go: ");
// reads an arbitrary number of integers that are in the range 1 to 100
int n;
while (scan.hasNext()) {
n = scan.nextInt();
if (n > 100) {
break;
}
int index = n / 10;
if (index == 10) {
index -= index;
break;
}
// for each integer read in, determine which range it is in and increment the
// corresponding element in the array
someArray[index] += 1;
}
scan.close();
}// end of getData
public static void displayChart(int[] someArray) {
// Print chart title with your name
System.out.println("\nDistribution Chart By simonshampoo" + "\n===================================");
// Print histogram.
for (int i = 0; i < 10; i++) {
int beginning = i * 10 + 1;
int ending = beginning + 9;
System.out.print(beginning + "-" + ending + "\t|");
for (int j = 0; j < someArray[i]; j++) {
System.out.print("*");
}
System.out.println();
}
} //
输出:
Enter a series of numbers between 1 and 100. Separate each number with a space.
Signal the end by entering a number outside of that range and then press enter.
Go: 10 20 30 40 50 60 70 80 90 11 22 33 44 55 66 77 88 99 1 31 31 45 98 99 100 500
Distribution Chart By simonshampoo
===================================
1-10 |*
11-20 |**
21-30 |**
31-40 |****
41-50 |***
51-60 |**
61-70 |**
71-80 |**
81-90 |**
91-100 |****
See you later!!
我有 10 和 1,都在 1-10 范围内,但是我从集合中删除了 1,没有星号出现,所以肯定是 10。非常感谢
我很确定不建议删除此处的问题,但我已经弄明白了。这是因为我在 if ( index == 10)
.
break
你的问题是这一行:
int index = n / 10;
对于n = 1-9,计算index = 0
,即该值应计入第1个桶,即桶0。
对于n = 10-19,计算index = 1
,即该值应计入第2个桶,即桶1。
如您所见,n = 10 被计入了错误的桶中。 20、30、40、... 90 也是如此,这就是为什么您需要忽略带有额外 if
(index == 10)
的 100,即使它也应该算在内。
自己看看:
- 尝试输入
9 999
并且1-10
有*
- 尝试输入
10 999
并且11-20
有*
(糟糕)。 - 尝试输入
100 999
并且不会有星星(哎呀)。
您的代码应该是:
while (scan.hasNext()) {
int n = scan.nextInt();
if (n < 1 || n > 100) {
break;
}
int index = (n - 1) / 10;
someArray[index]++;
}
使用代码,您可以按照说明输入 0
结束输入,但在问题代码中不起作用。