有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在数组中查找值的出现

我的问题是如何使用一种方法来计算这个数组中数字“8”和“88”的频率。看来我在评估员方法中所做的似乎不起作用。例如,如果“8”在数组中出现三次,则输出为“3”,而“88”的输出相同。 如果我错了,请给我指出正确的方向。非常感谢对我的问题的任何帮助

import java.util.Random;

public class ArrayPractice {
    private int[] arr;
    private final int MAX_ARRAY_SIZE = 300;
    private final int MAX_VALUE = 100;

    public ArrayPractice() {
        // initialize array
        arr = new int[MAX_ARRAY_SIZE];

        // randomly fill array with numbers
        Random rand = new Random(1234567890);
        for (int i = 0; i < MAX_ARRAY_SIZE; ++i) {
            arr[i] = rand.nextInt(MAX_VALUE) + 1;
        }
    }


    public void printArray() {
        for (int i = 0; i < MAX_ARRAY_SIZE; ++i) 
            System.out.println(arr[i]);
    }


    public int countFrequency(int value) {
    for (int i: MAX_VALUE) {
        if (i == 8) 
            i++;
    }



    public static void main(String[] args) {
        ArrayPractice ap = new ArrayPractice();

        System.out.println("The contents of my array are: ");
        ap.printArray();
        System.out.println("");

        System.out.println("The frequency of 8 is: " + ap.countFrequency(8));
        System.out.println("The frequency of 88 is: " + ap.countFrequency(88));

        }
    }
}

共 (4) 个答案

  1. # 1 楼答案

    当一个元素与value匹配时,需要迭代arr并增加一个变量

    public int countFrequency(int value) {
        int count = 0;
        for (int num : arr) {
            if (num == value) {
                count++;
            }
        }
        return count;
    }
    
  2. # 2 楼答案

    解决方案:

    public int countFrequency(int value) {
        int counter = 0; // here you will store counter of occurences of value passed as argument
        for (int i : arr) { // for each int from arr (here was one of your errors)
            if (i == value) // check if currently iterated int is equal to value passed as argument
                counter++; // if it is equal, increment the counter value
        }
        return counter; // return the result value stored in counter
    }
    

    说明:

    代码中的主要问题是countFrequency()方法,如果要使其正常工作,您需要更改的错误很少:

    1. 您将value作为参数传递,而您甚至没有在方法体中使用它

    2. for (int i : MAX_VALUE )-您的意思是迭代arr数组的元素(您可以将其读取为:对于arr数组中的每个int,执行以下操作:{…})

    3. if (i == 8) i++-这里你说的是这样的:检查数组中的当前元素(假设你的意思是MAX_VALUE是一个数组)是否等于8,如果是,则将该值增加1(如果是8,现在是9)。你在这里的意图是增加counter,计算8的发生率

    您可能想考虑对^ {CD1>}方法进行这些改进,使其正常工作。

  3. # 3 楼答案

    你有一个硬编码的种子,所以你的随机值在不同的运行中不会是随机的。您还根据8(而不是value)硬编码频率计数。但老实说,我建议您使用lambdas(从Java8开始)重新查看这段代码,它们可以用更少的代码编写数组生成、打印和计数例程。比如

    public class ArrayPractice {
        private int[] arr;
        private final int MAX_ARRAY_SIZE = 300;
        private final int MAX_VALUE = 100;
    
        public ArrayPractice() {
            // randomly fill array with numbers
            Random rand = new Random();
            arr = IntStream.generate(() -> rand.nextInt(MAX_VALUE) + 1)
                    .limit(MAX_ARRAY_SIZE).toArray();
        }
    
        public void printArray() {
            IntStream.of(arr).forEachOrdered(System.out::println);
        }
    
        public int countFrequency(int value) {
            return (int) IntStream.of(arr).filter(i -> i == value).count();
        }
    }
    
  4. # 4 楼答案

    当一个元素与i匹配时,需要迭代数组并增加一个计数器变量

    您所做的是增量i而不是计数器:

    if (i == 8)
      i++;
    }     // if i is 8 then i becomes 9 
    

    一个有效的例子:

    public int countFrequency(int i) {
    int count = 0;
    for (int num : arr) {
        if (num == i) {
            count++;
        }
    }
    return count;
    }