简体   繁体   English

Java-检查给定索引处的数组是否包含给定的int

[英]Java - check if an array at a given index contains a given int

I have an array that counts how many times each value from 1 to 6 appears in a dice simulator which "rolls a dice" 100 times. 我有一个数组,该数组计算从1到6的每个值在骰子模拟器中出现多少次,骰子模拟器“掷骰子”为100次。 My goal is to find the most frequent dice roll. 我的目标是找到最频繁的掷骰子。

This is my code so far, and everything works fine except the for-loop in the end which only outputs "6". 到目前为止,这是我的代码,除了最后只显示“ 6”的for循环外,其他一切都正常运行。

    Random dice = new Random();

    int diceThrow[] = new int[100];
    int throwsPerDice[] = new int[6];       

    for(int i = 0; i < 100; i++){
        diceThrow[i] = dice.nextInt(6) + 1;

        switch (diceThrow[i]){
            case 1:
                throwsPerDice[0]++;
                break;
            case 2:
                throwsPerDice[1]++;
                break;
            case 3:
                throwsPerDice[2]++;
                break;
            case 4:
                throwsPerDice[3]++;
                break;
            case 5:
                throwsPerDice[4]++;
                break;
            case 6:
                throwsPerDice[5]++;
                break;
            default:
                System.out.println("error");
        }

    }

    Arrays.sort(throwsPerDice);
    int max = throwsPerDice[throwsPerDice.length-1];
    int mostFrequent = 0;

    //Only outputs "mostFrequent = 6;" Why?
    for(int i = 0; i < throwsPerDice.length; i++){
        if(max == throwsPerDice[i]){
            mostFrequent = i+1;
        }
    }

    System.out.println("Most frequent dice roll : " + mostFrequent);

Any idea about what I'm doing wrong? 关于我在做什么错的任何想法吗? I'm trying to keep the code short and easy. 我试图使代码简短易行。 I'm in my first semester learning java so an unadvanced solution would be preferable. 我在第一学期学习Java,因此最好使用高级解决方案。

Also, is it possible to count the frequency of each diceThrow without using a switch/if statement? 另外,是否可以在不使用switch / if语句的情况下计算每个diceThrow的频率?

The main problem is that, once you've sorted throwsPerDice , you no longer know which of the counts refers to which die. 主要的问题是,对throwsPerDice进行排序throwsPerDice ,您将不再知道哪个计数指的是哪个骰子。 No matter what you do afterwards you cannot recover that information. 无论您以后做什么,都无法恢复该信息。

Your code always returns 6 because the highest count has been sorted into the final position in throwsPerDice . 您的代码始终返回6,因为最高计数已在throwsPerDice排在最终位置。

Let's say your array contains 假设您的数组包含

 [10, 20, 30, 20, 5, 15]

after the first loop. 在第一个循环之后。

Now the code sorts the array, so it becomes 现在,代码对数组进行了排序,因此它变为

[5, 10, 15, 20, 20, 30]

And max is initialized with the last value in the array: 30. max初始化为数组中的最后一个值:30。

Now the last loop iterates to find the index of the array containing the max element. 现在,最后一个循环进行迭代以查找包含max元素的数组的索引。 Of course it's always the last one, since you just sorted the array. 当然,它总是最后一个,因为您只是对数组进行了排序。

Rethink your algorithm: don't sort the array, but iterate over the array to find the max element, and its index. 重新考虑算法:不对数组进行排序,而是对数组进行迭代以找到max元素及其索引。

Just a note: your big switch statement should be replaced with 只需注意:大的switch语句应替换为

throwsPerDice[diceThrow[i] - 1]++;

remove this part of your code: 删除代码的这一部分:

Arrays.sort(throwsPerDice);
int max = throwsPerDice[throwsPerDice.length-1];
int mostFrequent = 0;

//Only outputs "mostFrequent = 6;" Why?
for(int i = 0; i < throwsPerDice.length; i++){
    if(max == throwsPerDice[i]){
        mostFrequent = i+1;
    }
}

and replace this: 并替换为:

int mostFrequent = 0;
for(int i = 0; i < throwsPerDice.length; i++){
    if(throwsPerDice[i] > throwsPerDice[mostFrequent]){
        mostFrequent = i;
    }
}
System.out.println("Most frequent dice roll : " + mostFrequent + 1);

this will work. 这将起作用。 Your code didnt work because your did not keep track of your dices when you use : Arrays.sort 您的代码无效,因为您在使用时没有跟踪骰子: Arrays.sort

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM