【问题标题】:return the index of the first element of the array A whose value is greater or equal to m返回数组 A 的第一个元素的索引,其值大于或等于 m
【发布时间】: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


    【解决方案1】:

    你可以像这样返回这个索引。

    public int getOverM(int m){
        for(int j=0; j<A.length;j++){
              if(m <= A[j]){
                flag=0;  
                break;
              }
              else flag=1;
        } 
        if(!flag)
          return j;
        else return -1;
    }
    

    【讨论】:

    • 它将返回第一个大于 m 的元素
    • 你能多放点光吗
    • 再次阅读问题
    • "getOverM 应该返回数组 A 中值大于或等于 m 的第一个元素的索引。"
    • 如果没有元素大于或等于 m,您的解决方案将失败
    【解决方案2】:

    我想这就是你一直在寻找的东西

    public int getOverM(int m){
            for(int j = 0; j < A.length; j++){
                  if(m <= A[j]){            //if the current element is greater than or equal to m then return the index .
                     return j;        
                  }
            } 
            return -1; // return -1 if the loop ends by not finding any element which is greater than or equal to m
        }
    

    【讨论】:

    • 天哪,你是一个救生员。我知道这不是一个复杂的代码...非常感谢!
    【解决方案3】:

    首先,getOverM() 应该返回“索引大于或等于 m”,所以你需要&lt;=

    否则,您所缺少的只是返回语句:

    public int getOverM(int m) {
        for(int j = 0; j < A.length; j++)
            if (m <= A[j])
                return j;
        return -1;
    }
    

    【讨论】:

      【解决方案4】:
      public int getOverM(int m){
                  int index = -1;
                  for(int j=0; j<A.length;j++){
                      if(m <= A[j])
                      {
                         index = j;
                         break;
                      }
                  }
                  return index;
      }
      

      【讨论】:

      • 这将返回最后一个索引,而不是第一个索引。
      • 等什么?请说得更具体一些,以便我提高自己。
      • 哦,对不起,我错了。这不会编译,因为j 不在return j 的范围内。我已将其读为return index,并且由于您在设置值时没有中断,因此循环继续并不断替换该值,因此返回值是匹配的 last 索引,而不是首先。
      • 我的坏索引是我想要返回的变量。
      猜你喜欢
      • 2019-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-27
      • 2018-04-06
      • 1970-01-01
      • 1970-01-01
      • 2021-09-04
      相关资源
      最近更新 更多