How to check if a specific value is present in an array?
I am creating a Yahtzee game. Every time I press the button "Roll The Dice" the values in my array change. The array contains 6 values (values between 1 to 6) for each of the six dice that are thrown. I have created a button that if it is clicked will count the number of "1" values in the array. How do I do such a thing? I'm a beginner in Java.
Re: How to check if a specific value is present in an array?
You declare a local variable that contains the number of occurrences (will be of type int I guess) and initialize it.
You loop through your array and within the loop (e.g. either for(...) or a while(...) loop) you use an if(...) statement to check if the current sample of your array is of the expected value.
If it is true then you increment your variable.
After the loop, the variable contains the number of occurrences of the specific number in the array.