gpt4 book ai didi

java - 如何通过返回主方法然后输入第二个方法将两个并行数组从一种方法移动到另一种方法?

转载 作者:行者123 更新时间:2023-11-30 02:44:47 26 4
gpt4 key购买 nike

我需要字符串和 double 组从 inputAccept 到表:

    inputAccept();

table(names, costs);

public static void inputAccept() {
Scanner scan = new Scanner(System.in);
String[] names = {"","","","","","","","","",""};
double[] costs = new double[10];

for (int i = 0; i < 10; i++)
{
System.out.println("Enter the name of item " + (i + 1) + ": ");
names[i] = scan.nextLine();
System.out.println("Enter item cost: ");
costs[i] = scan.nextDouble();
}
}

public static void table(String[] names, double[] costs) {
//insert code using the arrays
}

我知道这是错误的,但我不知道如何修复它。

最佳答案

您可以在 main 方法中创建两个数组,然后首先将它们传递到 inputAccept 方法中,然后将这两个数组传递到 table 方法中:

    public static void main(String[] args) {
double[] costs = new double[10];
String[] names = {"","","","","","","","","",""};
inputAccept(names, costs);
table(names, costs);
}

public static void inputAccept(String[] names, double[] costs) {
Scanner scan = new Scanner(System.in);
for (int i = 0; i < 10; i++)
{
System.out.println("Enter the name of item " + (i + 1) + ": ");
names[i] = scan.nextLine();
System.out.println("Enter item cost: ");
costs[i] = scan.nextDouble();
}
}

public static void table(String[] names, double[] costs) {
//insert code using the arrays
}

关于java - 如何通过返回主方法然后输入第二个方法将两个并行数组从一种方法移动到另一种方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40521046/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com