145k views
4 votes
Assign numMatches with the number of elements in userValues that equal matchValue. userValues has NUM_VALS elements. Ex: If userValues is {2, 1, 2, 2} and matchValue is 2 , then numMatches should be 3

User Marneylc
by
3.9k points

1 Answer

6 votes

Answer:

public class Match

{

public static void main(String[] args) {

final int NUM_VALS = 4;

int[] userValues = new int[NUM_VALS];

int matchValue, numMatches = 0;

userValues[0] = 2;

userValues[1] = 2;

userValues[2] = 1;

userValues[3] = 2;

matchValue = 2;

for(int i = 0; i < NUM_VALS; ++i) {

if(userValues[i] == matchValue)

numMatches++;

}

System.out.println("Match Value: " + matchValue + ", Number of Matches: " + numMatches);

}

Step-by-step explanation:

- Initialize the userValues array with length of NUM_VALS, which is initialized as 4

- Assign the numbers to the userValues array

- Update matchValue as 2

- Initialize a for loop that iterates through userValues array. If a number that is equal to matchValue found in array, increase the numMatches by 1

- Print the matchValue and numMatches

User Tora
by
4.2k points