简体   繁体   English

在数组中查找匹配值

[英]Finding matching values in an array

I have been tasked to assign numMatches with the number of elements in userValues that equal matchValue.我的任务是为 numMatches 分配 userValues 中等于 matchValue 的元素数。 userValues has NUM_VALS elements. userValues 有 NUM_VALS 个元素。 Its its going to be tested with the following inputs:它将使用以下输入进行测试:

matchValue: 2, userValues: {2, 1, 2, 2}匹配值:2,用户值:{2, 1, 2, 2}

matchValue: 0, userValues: {0, 0, 0, 0}匹配值:0,用户值:{0, 0, 0, 0}

matchValue: 10, userValues: {20, 50, 70, 100}匹配值:10,用户值:{20、50、70、100}

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 = -99; // Assign numMatches with 0 before your for loop

        matchValue = scnr.nextInt();
        for (i = 0; i < userValues.length; ++i) {
            userValues[i] = scnr.nextInt();
        }
        // Anything above this can't be changed.
        numMatches = 0;
        if (matchValue == numMatches) {
            numMatches = numMatches + 1;
        }
        // Anything below this can't be changed.
        System.out.println("matchValue: " + matchValue + ", numMatches: " + numMatches);
    }
}

Your output matchValue: 2, numMatches: 0你的输出matchValue: 2, numMatches: 0

Expected output matchValue: 2, numMatches: 3预期输出matchValue: 2, numMatches: 3

Your output matchValue: 0, numMatches: 1您的输出matchValue: 0, numMatches: 1

Expected output matchValue: 0, numMatches: 4预期输出matchValue: 0, numMatches: 4

Your output matchValue: 10, numMatches: 0你的输出matchValue: 10, numMatches: 0

Expected output matchValue: 10, numMatches: 0预期输出matchValue: 10, numMatches: 0

The only way I can get a different input to work is changing numMatches from 0 to match one of the other values in matchValue but not all 3 at the same time.我可以获得不同输入的唯一方法是将 numMatches 从 0 更改为匹配 matchValue 中的其他值之一,但不能同时匹配所有 3 个值。

You need to visit all elements of the array and increment the value of numMatches for each match.您需要访问数组的所有元素并为每个匹配增加numMatches的值。

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

Note: You can write numMatches = numMatches + 1 also as numMatches += 1 .注意:您可以将numMatches = numMatches + 1也写为numMatches += 1

A sample run after this change:此更改后的示例运行:

2
2 1 2 2
matchValue: 2, numMatches: 3

I think you only want to find number of values in array which are equal to a specified value.我认为您只想在数组中找到等于指定值的值的数量。 You can do it easily using java streams like this:您可以使用这样的 java 流轻松完成:

private static long getNumMatches(final int[] arr, final int matchValue) {
    return  Arrays.stream(arr)
            .filter(i -> i == matchValue)
            .count();
}

You can reuse this for as many input cases as you want:您可以根据需要将其重复用于任意数量的输入案例:

int[] input1 = {2, 1, 2, 2};
int matchValue1 = 2;
int[] input2 = {0, 0, 0, 0};
int matchValue2 = 0;
int[] input3 = {20, 50, 70, 100};
int matchValue3 = 10;

System.out.println("matchValue: " + matchValue1 + ", numMatches: " + getNumMatches(input1, matchValue1));
System.out.println("matchValue: " + matchValue2 + ", numMatches: " + getNumMatches(input2, matchValue2));
System.out.println("matchValue: " + matchValue3 + ", numMatches: " + getNumMatches(input3, matchValue3));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM