【问题标题】:Using Arrays as Method (Java)使用数组作为方法 (Java)
【发布时间】:2015-11-03 04:36:47
【问题描述】:

我在编译我的类时遇到问题。它给了我诸如“无法取消引用双精度”和“找不到符号”之类的错误。

潜水员是:

 public class MinilabArraysDriver{
    public static void main(String[ ] args){
       //create a 1d array of ints
       int[ ] array1d = { 8, 8, 7, 5, 3 };

       //call mean1d Method to find the mean (note the static call...)
       double theMean = MinilabArrays.mean1d(array1d);

       //print the original array and the mean
       System.out.print("The mean of: { ");
        for (int i=0; i<array1d.length; i++){
              if (i != 0)
                  System.out.print("   ");
             //print first for separation (except before first element)
             System.out.print(array1d[i]);
         }
        System.out.print(" }   is:  " + theMean);
        //------------------------------------------------------------
        System.out.println("\n\n");
        //create a 2d array of doubles
        double[ ][ ] array2d =  {{ 3.4,  5.1,  8.0},
                                 { 5.23,  8.2 },
                                 { 10.7 },
                                 { 2.9 }
                                };
        //call sum2d to get the sum
        double total = MinilabArrays.sum2d(array2d);

        //print the 2D array
        for (int row=0; row<array2d.length; row++){
             System.out.println();
             for (int col=0; col<array2d[row].length; col++)
                  System.out.print(array2d[row][col] + "\t");
            }
        //print the result
        System.out.println("\n\nTotal of 2d array is: " + total);
        System.out.println("\n\n");
       }
}

这是我正在尝试编写的课程:

public class MinilabArrays{
  public static double mean1d(int[ ] theMean){
    double total = 0;
    for (int i = 0 ;i < theMean.length ; i++){
            total = total + theMean [ i ];
            total = total / theMean.length;
            return total;
        }
    }

   public static double sum2d(double [ ] theSum){
    double total2, total3, total4 = 0;
    for(int row=0 ; row < theSum.length ; row++){
        for (int col=0 ; col<theSum[row].length ; col++)
            total2 = total2 + theSum[col];
        }
    total3 = total3 + theSum[row];
    total4 = total3 + total2;
    return total4;
   }
}

对于“mean1d”,我试图找到给定数字的平均值,对于“sum2d”,我试图找到给定数字的总和。

对不起,如果我的代码愚蠢或愚蠢。如果它不允许我编译,我无法检查它。

谢谢你帮助我!

【问题讨论】:

  • 你到底遇到了什么错误?
  • 当你编译它时,你会发现mean1d 中的循环并不是真正的循环。它只会执行一次然后离开。
  • 我得到“无法取消引用双精度”和“找不到符号”。
  • 您的代码中有很多错误,包括语法和逻辑错误。我建议使用 Eclipse 之类的 IDE 来帮助您进行调试。
  • 我一眼就看到的问题:mean1d() 正在循环内部返回,sum2d() 根据您传入的参数类型错误,total2total3 需要已初始化,row 需要在循环外声明,double 类型没有长度属性......

标签: java arrays class object methods


【解决方案1】:

您通过在参数中传递二维数组来调用 sum2d 方法,并将此方法声明为一维数组作为参数。

MinilabArrays.java

public static double sum2d(double[][] theSum) {
    double total2 = 0;
    for (int row = 0; row < theSum.length; row++) {
        for (int col = 0; col < theSum[row].length; col++){
            total2 = total2 + theSum[row][col];
        }
    }
    return total2;
}

【讨论】:

  • 知道了!感谢大家的帮助。面向对象编程对我来说很难理解。
【解决方案2】:

MinilabArrays.sum2d(array2d) 需要 double[] 而不是 double[][]。 而且此时 total3 = total3 + theSum[row]; 无法在 for 循环之外访问变量 row

【讨论】:

  • 我只是把“total3 = total3 + theSum[row];”在 for 循环内。 { for (int col=0 ; col
【解决方案3】:

第一个错误来自for (int col=0 ; col&lt;theSum[row].length ; col++) 这一行。您正在使用二维数组theSum[][],但将一维数组声明为方法参数。

另一个错误来自在其范围之外使用row。你在这个 for 循环 for(int row=0 ; row &lt; theSum.length ; row++) 中声明了它,但在 for 循环之外使用了它。

【讨论】:

  • 我该如何解决?数组本身的数字是双精度数,但 col 和 row 是 int。
  • @Paincakes 根本无法进行比较。你必须重新考虑你在做什么。
猜你喜欢
  • 1970-01-01
  • 2013-02-17
  • 2014-11-06
  • 2020-05-24
  • 2011-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多