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

标签 java arrays

我需要字符串和 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/

相关文章:

java - 使用 Graphics2D 绘制图像

java - cucumber AmbiguousStepDefinitionsException Java

java - 在 Gradle 构建的 installApp 阶段对每个文件执行操作

java - 当没有实现时,抽象方法如何工作?

c++ - 数组 (C++) : why use pointers over direct manipulation?

Java (Tomcat) : how to configure a cookieless subdomain to serve static content

java - 将字符串数组转换为 int 数组,然后将 int 转换为 *

jQuery .push() 不工作

c - 查询后在每个索引处查找最小值

javascript - 将数组从一种格式转换为另一种格式时出现问题