[英]Passing arrays between methods and calling them in main
I want to pass an array from one method to another and call that method on the array. 我想将数组从一个方法传递到另一个方法,然后在数组上调用该方法。 However, it shows where
increaseCost()
is not applicable for arguments. 但是,它显示了
increaseCost()
不适用于参数的地方。 How would I fix this code? 我将如何解决此代码?
I'm trying to pass double[] price
to increaseCost()
: 我正在尝试通过
double[] price
来increaseCost()
:
import java.util.*;
public class LabQuiz3 {
public static void main(String[] args) {
initialize();
increaseCost();
}
static void initialize() {
String[] name=new String[10];
System.out.println("Name of the grocery? ");
Scanner scan=new Scanner(System.in);
for (int i=0; i < 10; i++) {
String a = scan.next();
name[i]=a;
}
scan.close();
double[] price=new double[10];
System.out.println("Price of each grocery? ");
Scanner sc=new Scanner(System.in);
for (int j=0; j < 10; j++) {
double b=sc.nextDouble();
price[j]=b;
}
sc.close();
}
static void increaseCost(double[] price) {
for (int c=0; c <10; c++) {
double k=price[c] * .07;
price[c]=k;
}
System.out.println(price);
}
}
In your main method you have to get the prices
from the initialize()
method: 在您的主要方法中,您必须从
initialize()
方法获取prices
:
public static void main(String[] args) {
double[] prices = initialize();
increaseCost(prices);
}
And in the initialize method you have to return the prices
array: 在initialize方法中,您必须返回
prices
数组:
static double[] initialize() {
//all your code
...
return price;
}
A few other things to remark: 其他要注意的事项:
price
variable to prices
as it is a list of prices price
变量重命名为prices
因为它是价格列表 There are other things, but it's probably just a lab quiz. 还有其他事情,但这可能只是实验室测验。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.