通过方法传递数组参数 [英] Passing an array parameter through a method

查看:92
本文介绍了通过方法传递数组参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class sum
{
   public double getTotal(double[] doubleNumbers)
   {
       double sum = 0;
       for(int i = 0; i < doubleNumbers.length; i++)
       {
          sum = sum + doubleNumbers[i];
       }
       return sum;
   }
}


public class testAry 
{
   public static void main(String[] args) 
   {
       sum sumT = new sum();
       System.out.print(sumT.getTotal());                     
   }
}



getTotal方法需要采用双精度数组..
我该如何通过他们?我只是似乎无法正确理解语法. :doh:



The getTotal method needs to take an array of doubles..
how do i go about passing them? I just can''t seem to get the syntax right. :doh:

推荐答案

您可以执行以下操作:

方式1:
You can do something like this:

WAY 1:
double[] numbers = new double[2];
numbers[0] = 2.2;
numbers[1] = 1.1;

sum sumT = new sum();
sumT.getTotal(numbers);



方式2:



WAY 2:

sum sumT = new sum();
sumT.getTotal(new double[]{1.1,2.2,3.3});


谢谢,lata方法正是我想要的,我不确定如何传递参数.


谢谢.
Thanks, the lata method was what i was looking for, i wasn''t sure how to pass the argument.


thanks.


这篇关于通过方法传递数组参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆