用数组在java中存储数据

ckocjqey  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(225)

问题是我不知道如何存储来自控制台的输入。
这是我的密码:

for (int j = 0; j < arr1.length; j++) {
    System.out.println("");
    System.out.print("Select product code:        ");
    arr1[j] = reader.next();

    if (arr1[j].contains("A001")){
        mouse = 100.00;
        System.out.print("Enter quantity:               ");
        qty = reader.nextInt();
        amount = qty * mouse;
        System.out.print("Amount:\t\t\t    " + amount);
    }
    if (arr1[j].contains("A002")) {
        monitor = 2500.00;
        System.out.print("Enter quantity:               ");
        qty = reader.nextInt();
        amount = qty * monitor;
        System.out.print("Amount:\t\t\t    " + amount);
    }
    if (arr1[j].contains("A003")) {
        keyboard = 200.00;
        System.out.print("Enter quantity:               ");
        qty = reader.nextInt();
        amount = qty * keyboard;
        System.out.print("Amount:\t\t\t    " + amount);
    }
    if (arr1[j].contains("A004")) {
        flashdisk = 300.00;
        System.out.print("Enter quantity:               ");
        qty = reader.nextInt();
        amount = qty * flashdisk;
        System.out.print("Amount:\t\t\t    " + amount);
    }
    if (arr1[j].contains("A005")) {
        harddisk = 1500.00;
        System.out.print("Enter quantity:               ");
        qty = reader.nextInt();
        amount = qty * harddisk;
        System.out.print("Amount:\t\t\t    " + amount);
    }

这里的代码必须是显示存储数据结果的代码行。

System.out.println("");
System.out.print("Add item (y/n) " );
yn = reader.next().charAt(0);

if (yn == 'y' || yn == 'Y') {           
    continue;
}       
else if (yn == 'n' || yn == 'N') {          
    System.out.println("Code\tDescription\tUnit Price\tQuantity\tAmount");
    System.out.print(arr1[j]);
    if (arr1[j].contains("A001")) {
        System.out.print("\tMouse");
        System.out.print("\t\t"+mouse);
        System.out.print("\t\t"+qty);
        System.out.print("\t\t"+amount);

        if (arr1[j].contains("A002")) {
            System.out.print("\t\nMonitor");
            System.out.print("\t\t"+mouse);
            System.out.print("\t\t"+qty);
            System.out.print("\t\t"+amount);
        }
    }
}

示例输入必须是:

Select product code: A004
Enter quantity: 2
Amount: 800
add item(y/n)?: y

Select product code: A001
Enter quantity: 1
Amount: 100
add item(y/n)?: n

然后它将被存储在一个数组中,这将是结果,但是我没有得到两个输入数组,而是只得到第二个输入数组,而不是两个。

Code    Description Unit Price  Quantity    Amount
A001    Mouse       100.0       1           100.0
7kqas0il

7kqas0il1#

每次读取用户输入时,下一步就是重写信息,因为您存储的是变量而不是数组。
创建如下对象:

class Product {
  private String code;
  private String name;
  private Long price;
  private Integer amount;

  public Long getPrice();
  public Integer getAmount();
  public void setPrice(Long price) { this.setPrice(price); }
  public void setAmount(Integer amount) { this.setAmount(amount); }
...
}

然后,创建一个 Product [] productArr = new Product[anyLength]; 例如,当您检查输入是否为a005时,您将使用以下信息存储对象:

if (arr[j].contains("A005")) { 
        Product newProduct = new Product();
        newProduct.setPrice(1500.00);
        newProduct.setCode("A005");
        qty = reader.nextInt();
        amount = qty * newProduct.getPrice();
        newInfo.setAmount(amount);
        productArr[j] = newProduct;
}

当您需要打印它们时,您只需通过索引进行访问 productArr[j].getAmount() 例如。
作为建议尝试,但是将来如果您想要更好的方法,最好使用map<string,product>将a001Map到您的产品,a002…,a003。。。当你需要打印的时候,你只需要按一下键。

相关问题