Java:从另一个方法传递数组
我有一个项目,我有四种方法。第二种和第三种方法(分别使用和不使用)都有用户输入数据的数组 下面是第二种方法的示例。第三个很像Java:从另一个方法传递数组,java,Java,我有一个项目,我有四种方法。第二种和第三种方法(分别使用和不使用)都有用户输入数据的数组 下面是第二种方法的示例。第三个很像 public static void billWithoutGardens() { Scanner input = new Scanner(System.in); double withoutGardens[] = new double[12]; System.out.println("Please enter energy bills for
public static void billWithoutGardens()
{
Scanner input = new Scanner(System.in);
double withoutGardens[] = new double[12];
System.out.println("Please enter energy bills for year without rooftop gardens");
// 12 part array, one for each month
System.out.print("Please enter energy bill for January: ");
withoutGardens [0] = input.nextDouble();
System.out.print("Please enter energy bill for February: ");
withoutGardens [1] = input.nextDouble();
System.out.print("Please enter energy bill for March: ");
withoutGardens [2] = input.nextDouble();
System.out.print("Please enter energy bill for April: ");
withoutGardens [3] = input.nextDouble();
System.out.print("Please enter energy bill for May: ");
withoutGardens [4] = input.nextDouble();
System.out.print("Please enter energy bill for June: ");
withoutGardens [5] = input.nextDouble();
System.out.print("Please enter energy bill for July: ");
withoutGardens [6] = input.nextDouble();
System.out.print("Please enter energy bill for August: ");
withoutGardens [7] = input.nextDouble();
System.out.print("Please enter energy bill for September: ");
withoutGardens [8] = input.nextDouble();
System.out.print("Please enter energy bill for October: ");
withoutGardens [9] = input.nextDouble();
System.out.print("Please enter energy bill for November: ");
withoutGardens [10] = input.nextDouble();
System.out.print("Please enter energy bill for December: ");
withoutGardens [11] = input.nextDouble();
System.out.println(Arrays.toString(withoutGardens));
return;
我尝试使用第四种方法来计算两个数组中每个元素之间的差异。我不知道如何做到这一点,在四处搜索之后,我转向Stackoverflow的好人
第四种方法也是如此
public static void calculateSavings(double withoutGardens[], double withGardens[])
{
}
为了达到我计算差值的目的,这是开始使用该方法的正确方法吗
编辑-我如何将第四个方法调用回main
calculateSavings(withoutGardens[], withGardens[]);
这就是我现在得到它的方式,我得到了错误,它“无法解析为变量”。创建一个新数组,其大小与没有花园的
相同,然后执行一个循环,并将新数组的元素设置为没有花园的和有花园的元素的差异
同样,在方法中,您希望返回创建的数组,即不带花园
,因为它是billWithoutGardens()
中的局部变量,在该方法之外不可用
将方法签名更改为:
public static double[] billWithoutGardens()
public static double[] billWithoutGardens() {
}
public static double[] calculateSavings(double[] withoutGardens, double[] withGardens)
最后一行是:
return withoutGardens;
是的,您是正确的。您必须将2数组传递给第四个函数,然后才能将结果数组返回给
public static double[] calculateSavings(double withoutGardens[], double withGardens[])
{
double difference_array[withoutGardens.length()];
//.....some code goes here for calculations
return difference_array;
}
double[]diff=新的双精度[12];
对于(int i=0;i<12;i++){
diff[i]=没有花园[i]-有花园[i];
}
返回差;
我相信我能帮你回答你的问题
在回答问题之前,让我指出以下参数,而不是:
double withoutGardens[], double withGardens[]
您应改为使用:
double[] withoutGardens, double[] withGardens
以确保参数的类型是数组,并且括号不仅仅在名称中
现在,要确定每个元素之间的差异,必须循环遍历它们。这可以通过简单的for循环或foreach循环完成。在我看来,for循环比foreach循环更容易,因为您使用的是2个数组,而for循环可以为索引提供一个整数,以便两者都使用。如果使用foreach循环,则必须使用方法查找当前索引
如果使用for循环,它将如下所示:
public static double[] calculateSavings(double[] withGardens, double[] withoutGardens) {
double[] difference = new double[];
for (int i = 0; i < withGardens.size(); i++) {
difference[i] = withGardens[i] - withoutGardens[i];
}
return difference;
}
public static double[]计算布局(double[]带花园,double[]不带花园){
双[]差=新的双[];
对于(inti=0;i
我希望这有帮助 我知道一个答案已经被接受了,不过,有几件事我想提一下,你/其他人可能会从中受益。首先,让billWithoutGardens()返回用户输入的双倍数组,因此让我们将其签名更改为:
public static double[] billWithoutGardens()
public static double[] billWithoutGardens() {
}
public static double[] calculateSavings(double[] withoutGardens, double[] withGardens)
接下来,您可以通过在方法中使用循环大大简化数据输入,例如:
//an array of months
String[] months = {"January", "February", "March", "April", "May", "June", "July", "September", "October", "November", "December"};
System.out.println("Please enter energy bills for year without rooftop gardens");
//loop through the months and store the values in your array
for(int i=0; i<months.length; i++) {
System.out.print("Please enter energy bill for " + months[i] + ": ");
withoutGardens[i] = input.nextDouble();
//!!!!!!!!!!!!!
//also remember to skip newline character added when user pressed ENTER
//you can consume it with this call, which will read newline character
input.nextLine();
}
接下来,为了计算节省,我们还将在calculateSavings()中返回一个数组,因此将其签名更改为:
public static double[] billWithoutGardens()
public static double[] billWithoutGardens() {
}
public static double[] calculateSavings(double[] withoutGardens, double[] withGardens)
要计算差异,请创建一个数组,该数组将保存每个月的差异,并循环遍历各个月:
double[] difference = new double[withoutGardens.length];
for (int i=0; i<difference.length; i++) {
difference[i] = withoutGardens[i] - withGardens[i];
}
同样,您可以重写其他方法。然后,要在main()中使用它们,可以使用以下内容:
public static void main(String[] args) {
double[] wGardens = billWithGardens();
double[] woGardens = billWithoutGardens();
double[] savings = calculateSavings(woGardens, wGardens);
System.out.println(Arrays.toString(savings));
}
坏标签。应该是这样的,看起来不错。你试过了吗?应该是双倍回报,而不是无效的想法哦,我想这确实有效。我不知道如何将第四个方法调用到我的main中,但是因为它使用了这些特定的参数。计算雕刻(无花园[],有花园[]);我在这方面遇到了错误,用calculateSavings(没有花园,有花园)调用它代码>啊,我的错。显然,您不必更改参数名称。不管怎样,它都不是传统的。