Results 1 to 2 of 2
- 01-05-2013, 12:32 PM #1
Member
- Join Date
- Jan 2013
- Posts
- 1
- Rep Power
- 0
How to check if a specific value is present in an array?
Hi there,
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.
Thank you!
- 01-05-2013, 12:53 PM #2
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
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.Last edited by Sierra; 01-05-2013 at 12:55 PM.
I like likes!.gif)
Similar Threads
-
getting specific information from the array list
By elvis0288 in forum New To JavaReplies: 7Last Post: 03-05-2012, 12:40 PM -
array with specific needs
By BeginnerProgrammer in forum Java AppletsReplies: 9Last Post: 06-21-2011, 03:14 AM -
How to check if a specific data in a collum
By mike_ledis in forum JDBCReplies: 0Last Post: 04-27-2011, 02:09 PM -
Getting specific hash in a 2D-Array
By benn22 in forum New To JavaReplies: 0Last Post: 03-18-2011, 07:24 AM -
How to: Select specific array indices?
By louist in forum New To JavaReplies: 3Last Post: 03-07-2011, 10:21 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks