嗨我现在遇到了我的代码问题,因为我已经被困了一段时间了,我是java编程的新手。我希望代码输出从0到9而不是1-10,我的代码和输出显示在下面:
import java.util.Arrays;
public class RandNumGenerator {
public static int RandInt(){
double n = Math.random()*10;
return (int) n;
}
public static void randTest(int n){
int [] counts = new int [10];
int sampleSize = 10000;
int RandNum = RandInt();
System.out.println ("Sample Size: " + sampleSize);
String[] intArray = new String[] {"Value","Count","Expected","Abs Diff","Percent Diff"};
System.out.println(Arrays.toString(intArray));
for(int i=0;i<n;i++){
counts[ RandNum ] = counts[ RandNum ] + 1;
System.out.println(counts[RandNum]);
}
}
public static void main(String[] args) {
randTest(10);
}
}
输出为1-10,而不是0-9:
Sample Size: 10000
[Value, Count, Expected, Abs Diff, Percent Diff]
1
2
3
4
5
6
7
8
9
10