gpt4 book ai didi

java - 在 Java 中操作数组。陷入困难的家庭作业

转载 作者:行者123 更新时间:2023-12-01 06:55:56 25 4
gpt4 key购买 nike

我是 Java 新手,本学期正在学习编程类(class)。我们有作业要交,我很挣扎。我很高兴这对于经验丰富的程序员来说很容易,但对我来说这是一个令人头疼的问题。这是第一个问题。

public int countInRange(int[] data, int lo, int hi)

For this, you have to count the number of elements of the array, data, that lie in the range >>lo to hi inclusive, and return the count. For example, if data is the array {1, 3, 2, 5, 8} >>then the call

countInRange(data, 2, 5)

should return 3 because there are three elements, 3, 2 and 5 that lie in the range 2 .. 5.

这是我到目前为止所做的:

/**
* Count the number of occurrences of values in an array, R, that is
* greater than or equal to lo and less than or equal to hi.
*
* @param data the array of integers
* @param lo the lowest value of the range
* @param hi the highest value of the range
* @return the count of numbers that lie in the range lo .. hi
*/
public int countInRange(int[] array, int lo, int hi) {
int counter = 0;
int occurrences = 0;
while(counter < array.length) {
if(array[counter] >= lo) {
occurrences++;
}
counter++;
}
return occurrences;
}

最佳答案

if(array[counter] >= lo && conditionforhighcheck)
{
//Then only update occurence count
}

因为作业的原因,我没有输入代码。我给了指点。

关于java - 在 Java 中操作数组。陷入困难的家庭作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11999163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com