【发布时间】:2016-04-11 23:58:29
【问题描述】:
我正在制作一个直方图程序,其中我有一个生成随机数的数组,另一个将它们变成一个数组,最后一个尝试使用该数组并告诉每个 [] 中有多少个字符的数组。我的问题是我找不到一种方法来计算每个数组元素中有多少个字符并打印出来。我正在尝试使用 .length 函数,但它似乎不起作用。还有另一种方法可以做到这一点吗? 这是我的代码。我的问题是我的最后一个方法,在我的主要方法之前。
package arrayhistogram;
/**
*
* @author Dominique
*/
public class ArrayHistogram {
public static int randomInt(int low, int high){
double x =Math.random ()* (high - low)+low;
int x1=(int)x;
return x1;
}
public static int[] randomIntArray(int n){
int[] a = new int[n];
for (int i=0; i<n; i++) {
a[i] = randomInt(-5, 15);
}
return a;
}
public static int[] arrayHist () {
int[] x=randomIntArray(30);
for (int i = 0; i < x.length; i++) {
System.out.println(x[i].length);
}
return x;
}
public static void main(String[] args) {
arrayHist();
}
}
【问题讨论】:
-
System.out.println(x[i])? -
@TimBiegeleisen 我认为 OP 想要位数