in java Set numMatches to the number of elements in userValues (having NUM_VALS
ID: 3798070 • Letter: I
Question
in java
Set numMatches to the number of elements in userValues (having NUM_VALS elements) that equal matchValue. Ex: If matchValue = 2 and userValues = {2, 2, 1, 2}, then numMatches = 3.
import java.util.Scanner;
public class FindMatchValue {
public static void main (String [] args) {
final int NUM_VALS = 4;
int[] userValues = new int[NUM_VALS];
int i = 0;
int matchValue = 0;
int numMatches = -99; // Assign numMatches with 0 before your for loop
userValues[0] = 2;
userValues[1] = 2;
userValues[2] = 1;
userValues[3] = 2;
matchValue = 2;
/* student solution goes here */
System.out.println("matchValue: " + matchValue + ", numMatches: " + numMatches);
return;
}
}
Explanation / Answer
FindMatchValue.java
import java.util.Scanner;
public class FindMatchValue {
public static void main (String [] args) {
final int NUM_VALS = 4;
int[] userValues = new int[NUM_VALS];
int i = 0;
int matchValue = 0;
int numMatches = -99; // Assign numMatches with 0 before your for loop
userValues[0] = 2;
userValues[1] = 2;
userValues[2] = 1;
userValues[3] = 2;
matchValue = 2;
/* student solution goes here */
numMatches=0;
for(i=0;i<userValues.length;i++)
{
if(matchValue==userValues[i])
{
numMatches++;
}
}
//Displaying the result
System.out.println("matchValue: " + matchValue + ", numMatches: " + numMatches);
return;
}
}
_________________
output:
matchValue: 2, numMatches: 3
______________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.