如何用一个2D数组创建4个数组以及如何扫描它们

时间:2019-01-01 19:46:46

标签: java arrays

我必须制作一个有关商店和销售的程序。我必须从客户那里读取值,并将它们放在不同的数组和数量中。一个阵列中的商店,其他阵列中销售的产品以及产品的数量和所有产品的价格必须在2d阵列中。 我在2D阵列中挣扎。

我试图读取所有数组的值,但是当我打印2D数组时,它总是给我一个错误。我认为我的问题出在“ for”中,但是我不确定,因为我是java的新手。

public static void main(String[] args) {
    Scanner read = new Scanner(System.in);
    int sells = 0;
    String store = "";

    System.out.print("How many sells did you make: ");
    sells = read.nextInt();

    String[] arrStores = new String[sells];
    String[] arrProduct = new String[sells];
    double[][] arrAmountPrice = new double[sells][2];

    for (int i = 0; i < sells; i++) {

        System.out.print("Store: ");
        arrStores[i] = ler.next();

        System.out.print("Product sold: ");
        arrProduct[i] = ler.next();

        System.out.print("Amount of product sold: ");
        arrAmountPrice[i][0] = ler.nextDouble();

        System.out.print("Price of all the product: ");
        arrAmountPrice[i][1] = ler.nextDouble();


    }

1 个答案:

答案 0 :(得分:0)

首先,您需要导入'java.util.Scanner'或'java.util。*'类以获取对Scanner对象的访问权限,然后您要做的就是将'ler'对象替换为'read' '。

import java.util.*;

public class Test{

    public static void main(String[] args) {
        Scanner read = new Scanner(System.in);
        int sells = 0;
        String store = "";

        System.out.print("How many sells did you make: ");
        sells = read.nextInt();

        String[] arrStores = new String[sells];
        String[] arrProduct = new String[sells];
        double[][] arrAmountPrice = new double[sells][2];

        for (int i = 0; i < sells; i++) {

            System.out.print("Store: ");
            arrStores[i] = read.next();

            System.out.print("Product sold: ");
            arrProduct[i] = read.next();

            System.out.print("Amount of product sold: ");
            arrAmountPrice[i][0] = read.nextDouble();

            System.out.print("Price of all the product: ");
            arrAmountPrice[i][1] = read.nextDouble();

        }
    }
}