【发布时间】:2016-07-27 07:29:59
【问题描述】:
public class Accumulator {
private int[] A;
public Accumulator(int[] X) {
A= new int[X.length];
for (int i=0; i<X.length; i++){
A[i] = X[i];
}
}
public int getOverM(int m){
for(int j=0; j<A.length;j++){
if(m < A[j])
{
}
}
}
public static void main(String args[]){ // you can use the main method to test your code
int[] A = {2,4,3,5,8};
int r=new Accumulator(A).getOverM(3); //change argument to test different cases
System.out.println(r);
}
}
您好,我一直在研究这个很长时间,我知道这看起来很容易,但我无法理解它......我只能更改方法 getOverM() 中的代码,我无法编辑其他方法。 我想过使用 if 语句,但我只是不知道如何编写代码来显示与 m 相比的下一个最大索引号。
问题:考虑以下 Accumulator 类缺少方法的代码 'getOverM(int m)'。 getOverM 应该返回数组 A 的第一个元素的索引,其 值大于或等于 m。
如果 A 中没有元素的索引大于或等于 m,则该方法 应该返回-1。 例如,如果 A 是数组 {2,4,3,5,8} 那么 getOverM(3) 将返回 1 getOverM(2) 将返回 0 getOverM(7) 将返回 4 getOverM(20) 将返回 -1
(提示:数组A的长度由A.length给出)
为getOverM方法的主体插入代码。
【问题讨论】:
标签: java arrays oop if-statement for-loop