Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java家庭作业-存储计数到一个数组_Java_Arrays_Store - Fatal编程技术网

Java家庭作业-存储计数到一个数组

Java家庭作业-存储计数到一个数组,java,arrays,store,Java,Arrays,Store,我正试图编写一个程序,将随机数的计数存储到一个一维数组中。我在为数组分配数字计数时遇到了问题。这是一个逻辑错误,它编译得很好。谢谢 public class PP67 { public static void main(String[] args) { int zero =0, one=0, two=0, three=0, four=0, five=0, six=0, seven=0, eight=0, nine=0; int[] counts = ne

我正试图编写一个程序,将随机数的计数存储到一个一维数组中。我在为数组分配数字计数时遇到了问题。这是一个逻辑错误,它编译得很好。谢谢

public class PP67 {
    public static void main(String[] args) {

        int zero =0, one=0, two=0, three=0, four=0, five=0, six=0, seven=0, eight=0, nine=0;
        int[] counts = new int[10];//create array
        int i;

        for(i = 0; i < 100; i++) {

            int rand = (int)(Math.random()*10);     //generate 100 random ints between 0-9

            if (rand == 1){                    //assign the random number counter to the array
                one++;
                counts[0] = one;


            } else if (rand == 2) {
                two++;
                counts[1] = two;


            } else if (rand == 3) {
                three++;
                counts[2] = three;


            } else if (rand == 4) {
                four++;
                counts[3] = four;


            } else if (rand == 5) {
                five++;
                counts[4] = five;


            } else if (rand == 6) {
                six++;
                counts[5] = six;


            } else if (rand == 7) {
                seven++;
                counts[6] = seven;


            } else if (rand == 8) {
                eight++;
                counts[7] = eight;


            } else if (rand == 9) {
                nine++;
                counts[8] = nine;


            } else if (rand == 0) {
                zero++;
                counts[9] = zero;

            }

        }
        System.out.println("The count for number 1 is: " + counts[0]); //need outside count loop
        System.out.println("The count for number 2 is: " + counts[1]);
        System.out.println("The count for number 3 is: " + counts[2]);
        System.out.println("The count for number 4 is: " + counts[3]);
        System.out.println("The count for number 5 is: " + counts[4]);
        System.out.println("The count for number 6 is: " + counts[5]);
        System.out.println("The count for number 7 is: " + counts[6]);
        System.out.println("The count for number 8 is: " + counts[7]);
        System.out.println("The count for number 9 is: " + counts[8]);
        System.out.println("The count for number 0 is: " + counts[9]);
    }
}
公共类PP67{
公共静态void main(字符串[]args){
int零=0,一=0,二=0,三=0,四=0,五=0,六=0,七=0,八=0,九=0;
int[]计数=新的int[10];//创建数组
int i;
对于(i=0;i<100;i++){
int rand=(int)(Math.random()*10);//在0-9之间生成100个随机整数
如果(rand==1){//将随机数计数器分配给数组
1++;
计数[0]=1;
}else if(rand==2){
两个++;
计数[1]=两个;
}否则如果(兰德==3){
三个++;
计数[2]=三次;
}else if(rand==4){
四++;
计数[3]=四;
}否则如果(兰德==5){
五++;
计数[4]=五;
}否则如果(兰德==6){
六++;
计数[5]=六;
}else if(rand==7){
七++;
计数[6]=七;
}否则如果(兰德==8){
八++;
计数[7]=八;
}否则如果(兰德==9){
九++;
计数[8]=九;
}else if(rand==0){
零++;
计数[9]=零;
}
}
System.out.println(“数字1的计数为:“+counts[0]);//需要外部计数循环
System.out.println(“数字2的计数为:“+counts[1]);
System.out.println(“数字3的计数为:“+counts[2]);
System.out.println(“数字4的计数为:“+counts[3]);
System.out.println(“数字5的计数为:“+counts[4]);
System.out.println(“数字6的计数为:“+counts[5]);
System.out.println(“数字7的计数为:“+counts[6]);
System.out.println(“数字8的计数为:“+counts[7]);
System.out.println(“数字9的计数为:“+counts[8]);
System.out.println(“数字0的计数为:“+counts[9]);
}
}
问题在于:

int rand = (int)Math.random()*10;
演员的角色扮演比乘法更重要

由于
Math.random
[0,1[
(作为状态)中返回一个双精度,因此将其转换为
int
之后将始终以0结束。因此,您总是执行
0*10
。因此
rand
始终为0

首先将随机数乘以10,然后将其转换为
int

int rand = (int)(Math.random()*10);
也可以使用类中的方法

1) 您不需要在变量1、变量2、变量3等和数组计数中保留相同的信息

2) 表达式
int rand=(int)(Math.random()*10)
总是返回
0
。要在[0,9]中生成随机数,最好使用

Random r = new Random();
int rand = r.nextInt(10);
3) 您不需要所有这些
if-else-if
条件,您可以使用随机数作为数组计数的索引

private final static int REP = 100;
private final static int MAX = 10;
public static void main(String[] args) {

    Random rand = new Random(System.currentTimeMillis());

    int[] counts = new int[MAX];
    for(int i=0; i<REP; i++)
    {
        int n = rand.nextInt(MAX);
        counts[n]++;
    }

    for(int i=0; i<MAX; i++)
    {
        System.out.printf("The count for number %d is: %d \n", i, counts[i] );
    }
}
private final static int REP=100;
私有最终静态int MAX=10;
公共静态void main(字符串[]args){
Random rand=新的Random(System.currentTimeMillis());
int[]计数=新的int[MAX];

对于(int i=0;i具体来说,您遇到了什么问题?在将其设置为数组之前,请尝试递增。10个数组插槽中的每个插槽的输出都显示为0。这实际上不是为我计算随机数。我需要程序计算随机数出现的次数。在预先递增并更改随机数之后优先,我能够运行这个。谢谢大家。@RUJordan n'zouf对于那些不知道的人,
[0,1[
的意思是“大于或等于零,小于一”。有些人可能更习惯于
[0,1)
private final static int REP = 100;
private final static int MAX = 10;
public static void main(String[] args) {

    Random rand = new Random(System.currentTimeMillis());

    int[] counts = new int[MAX];
    for(int i=0; i<REP; i++)
    {
        int n = rand.nextInt(MAX);
        counts[n]++;
    }

    for(int i=0; i<MAX; i++)
    {
        System.out.printf("The count for number %d is: %d \n", i, counts[i] );
    }
}