【问题标题】:How do you invoke a method? [closed]你如何调用一个方法? [关闭]
【发布时间】:2014-03-07 22:43:58
【问题描述】:

当我在eclipse中调用它时,我将如何调用它来打印出来???

public class Arraymini {

    public static void main(String [] args){
        int [] testArray1= {1,6,3,9,2};
        double [] testArray2= {2.3, 8.66, 6.5, -9.2};
    }

    public static void printArray(int []k){
        for(int i=0; i<k.length; i++){
        System.out.println(k[i]+" ");
        }
    }

    public static void printArray(double[]g){
        for(int i=0; i<g.length; i++){
            System.out.println(g[i]+" ");
        }
    }

}

也是他们的一种方式,我想改变 foror 循环让它连续打印出来?????

【问题讨论】:

  • 询问代码的问题必须表明对所解决问题的最低理解。
  • 你甚至没有调用该方法。
  • noooo duhhh 这就是我要问的问题!

标签: java for-loop


【解决方案1】:

在您的 main 方法中,您可以使用以下方法调用您的方法:

public static void main(String[] args)
{
    //-- Your original code up above

    printArray(testArray1); //For the array of ints
    printArray(testArray2); //For the array of doubles
}

【讨论】:

  • 第一个方法打印一个整数数组,第二个打印一个双精度数组。
  • @kabb 谢谢,没意识到 :D
【解决方案2】:
public class Arraymini {

public static void main(String [] args){
    int [] testArray1= {1,6,3,9,2};
    double [] testArray2= {2.3, 8.66, 6.5, -9.2};
    printArray(testArray1);
    printArray(testArray2);
}

public static void printArray(int []k){
    for(int i=0; i<k.length; i++){
    System.out.println(k[i]+" ");
    }
}

public static void printArray(double[]g){
    for(int i=0; i<g.length; i++){
        System.out.println(g[i]+" ");
    }
}

}

【讨论】:

    【解决方案3】:
    public static void main(String [] args){
        int[] testArray1 = {1,6,3,9,2};
        double[] testArray2 = {2.3, 8.66, 6.5, -9.2};
        printArray(testArray1);
        printArray(testArray2);
    }
    

    【讨论】:

      【解决方案4】:

      main() 函数中添加以下内容:

      printArray(testArray1);
      printArray(testArray2);
      

      【讨论】:

        猜你喜欢
        • 2014-03-15
        • 1970-01-01
        • 2016-06-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-17
        相关资源
        最近更新 更多