Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何通过返回主方法然后输入第二个方法,将两个并行数组从一个方法移动到另一个方法?_Java_Arrays - Fatal编程技术网

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

Java 如何通过返回主方法然后输入第二个方法,将两个并行数组从一个方法移动到另一个方法?,java,arrays,Java,Arrays,我需要字符串和双数组从inputAccept转到表: inputAccept(); table(names, costs); public static void inputAccept() { Scanner scan = new Scanner(System.in); String[] names = {"","","","","","","","","",""}; double[] costs =

我需要字符串和双数组从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
    }
inputAccept();
表(名称、费用);
公共静态void inputAccept(){
扫描仪扫描=新扫描仪(System.in);
字符串[]名称={“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”};
双倍[]成本=新的双倍[10];
对于(int i=0;i<10;i++)
{
System.out.println(“输入项目名称”+(i+1)+“:”;
名称[i]=scan.nextLine();
System.out.println(“输入项目成本:”);
costs[i]=scan.nextDouble();
}
}
公共静态无效表(字符串[]名称,双[]成本){
//使用数组插入代码
}

我知道这是错误的,但我不知道该怎么做来修复它。

这种方法就像婴儿的屁股一样干净

private class InputResult {
    //generate constructor with both fields
    String[] names; //generate getter
    double[] costs; //generate getter
}
然后

公共类主{
静态字符串[]名称={“”、、、、、、、、、、、、、、、、、};
静态双[]成本=新双[10];
公共静态void main(字符串[]args){
inputAccept();
表(名称、费用);
}
公共静态void inputAccept(){
扫描仪扫描=新扫描仪(System.in);
对于(int i=0;i<10;i++)
{
System.out.println(“输入项目名称”+(i+1)+“:”;
名称[i]=scan.next();
System.out.println(“输入项目成本:”);
costs[i]=scan.nextDouble();
}
}
公共静态无效表(字符串[]名称,双[]成本){
for(字符串名称:名称){
System.out.println(名称);
}
用于(双倍价值:成本){
系统输出打印项次(值);
}
}
}

您只需在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
    }
publicstaticvoidmain(字符串[]args){
双倍[]成本=新的双倍[10];
字符串[]名称={“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”};
输入接受(名称、成本);
表(名称、费用);
}
公共静态void inputAccept(字符串[]名称,双[]成本){
扫描仪扫描=新扫描仪(System.in);
对于(int i=0;i<10;i++)
{
System.out.println(“输入项目名称”+(i+1)+“:”;
名称[i]=scan.nextLine();
System.out.println(“输入项目成本:”);
costs[i]=scan.nextDouble();
}
}
公共静态无效表(字符串[]名称,双[]成本){
//使用数组插入代码
}

首先,看起来您正在根据用户输入创建“项目”。我认为建模类
应该是第一步:

public final class Item {
    private final String name;
    private final double cost;

    public Item(String name, double cost) {
        this.name = name;
        this.cost = cost;
    }

    public String getName() {
        return name;
    }

    public double getCost() {
        return cost;
    }
}
然后,
inputAccept()
变成:

public static Item[] inputAccept() {
    Item[] items = new Item[10];

    try (Scanner scan = new Scanner(System.in)) {
        for (int i = 0; i < items.length; i++) {
            System.out.println("Enter the name of item " + (i + 1) + ": ");
            String name = scan.next();
            System.out.println("Enter item cost: ");
            double cost = scan.nextDouble();

            items[i] = new Item(name, cost);
        }
    }

    return items;
}
最后的用法是

public static void main(String[] args) {
    table(inputAccept());
}  

通过不创建并行数组,而是使用面向对象的功能,可以创建一个Java并使用两个字段的对象创建一个数组,也就是说,通过正确的方法创建一个数组。@GDaniels而不是nextLIne,您应该使用scan.next()Compile error
obj[0]
与参数
String[]
不兼容
obj[1]
double[]
不兼容。不要说强制转换它们,因为绕过编译器类型检查是一种糟糕的代码模式。创建一个类。我确实这么认为。你有没有想过在评论之前先阅读我的评论?但我想我错过了这个:编译器错误<代码>对象
不是类。@安德烈亚斯用你提到的类创建了一个更干净的解决方案。我有一个main方法。我只是发布了两个相关的方法。复制问题的代码,而不复制尝试表示主要方法的前两行代码,这很难回答问题。我现在编辑了答案,以包含更好的解决方案。我以前误解了这个问题。将向下投票改为向上投票。这是一个很好的解决方案,它可以保持并行数组,不需要静态字段,不需要额外的类,也不需要强制转换。这应该可以解决您的问题,但我希望这只是为了练习。为什么在初始值设定项中分配一个数组,而在方法中分配另一个数组?一致性是代码中的一件好事。这只是为了回答OP的问题。以后可以进行进一步的重新分解。。
public static Item[] inputAccept() {
    Item[] items = new Item[10];

    try (Scanner scan = new Scanner(System.in)) {
        for (int i = 0; i < items.length; i++) {
            System.out.println("Enter the name of item " + (i + 1) + ": ");
            String name = scan.next();
            System.out.println("Enter item cost: ");
            double cost = scan.nextDouble();

            items[i] = new Item(name, cost);
        }
    }

    return items;
}
public static void table(Item[] items) {
    // insert code using the arrays
}
public static void main(String[] args) {
    table(inputAccept());
}