在Java中使用数组存储数据

在Java中使用数组存储数据,java,arrays,Java,Arrays,问题是我不知道如何存储来自控制台的输入 这是我的密码: 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")){

问题是我不知道如何存储来自控制台的输入

这是我的密码:

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);
    }
示例输入必须是:

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

每次读取用户输入时,下一步就是覆盖信息,因为您存储的是变量而不是数组

创建一个对象,如:

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=newproduct[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()
进行访问即可


作为建议尝试,但将来如果您想要更好的方法,最好使用地图将A001映射到您的产品,A002…,A003。。。然后,当你需要打印它们时,你只需按键即可。

在结果部分,if在哪里检查这个A004?第二个块中的j来自哪里?我尝试过对A004执行if,但仍然得到相同的结果。我不明白你的想法,请你进一步告诉我。对不起,这件事我确实需要帮助(((首先,我们的解决方案行不通,仔细看看这个变量。它并不总是硬盘,对吧?其次,他没有要求使用像mapI这样的新机制,建议更改类的输出属性:Code-Description-Unit-Price-Quantity-amount他可以在Info对象中存储他想要的信息。地图只是一个建议。)n、 这意味着它是可选的。@JiggyPalconit如果这没有帮助,我将删除我的答案
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;
}