Sovi.AI - AI Math Tutor

Scan to solve math questions

QUESTION IMAGE

integer matchvalue is read from input. then, array uservalues is assign…

Question

integer matchvalue is read from input. then, array uservalues is assigned with num_vals integers read from input. organize the lines of code to assign nummatches with the number of elements in uservalues that equal matchvalue. ex: if the input is: 2 2 1 2 2 then the output is: matchvalue: 2, nummatches: 3 how to use this tool unused for (i = 0; i < uservalues.length; ++i) { } nummatches = nummatches + 1; if (uservaluesi == matchvalue) { } nummatches = 0; findmatchvalue.java import java.util.scanner; public class findmatchvalue { public static void main(string args) { scanner scnr = new scanner(system.in); final int num_vals = 4; int uservalues = new intnum_vals; int i; int matchvalue; int nummatches; matchvalue = scnr.nextint(); for (i = 0; i < uservalues.length; ++i) { uservaluesi = scnr.nextint(); }

Explanation:

Step1: Initialize numMatches

numMatches = 0;

Step2: Iterate through userValues

for (i = 0; i < userValues.length; ++i) {

Step3: Check for match

if (userValues[i] == matchValue) {

Step4: Increment numMatches

numMatches = numMatches + 1;

Step5: End loop

}

Answer:

import java.util.Scanner;

public class FindMatchValue {
    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);

        final int NUM_VALS = 4;
        int[] userValues = new int[NUM_VALS];
        int i;
        int matchValue;
        int numMatches;

        matchValue = scnr.nextInt();
        for (i = 0; i < userValues.length; ++i) {
            userValues[i] = scnr.nextInt();
        }

        numMatches = 0;
        for (i = 0; i < userValues.length; ++i) {
            if (userValues[i] == matchValue) {
                numMatches = numMatches + 1;
            }
        }

        System.out.println("matchValue: " + matchValue + ", numMatches: " + numMatches);
    }
}