[英]Storing data using Array in Java
The problem is I don't know how to store my inputs from console.问题是我不知道如何从控制台存储我的输入。
Here is my code:这是我的代码:
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);
}
and here is the code that must be the line of code where it will show the result of stored data.这是代码必须是将显示存储数据结果的代码行。
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);
}
}
}
The sample input must be:样本输入必须是:
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
then it will be stored in an array which would be the result but instead of getting both of the input arrays, I only get the 2nd input array instead both of them.然后它将存储在一个数组中,这将是结果,但我没有获取两个输入数组,而是只获取第二个输入数组而不是两个。
Code Description Unit Price Quantity Amount
A001 Mouse 100.0 1 100.0
Every time you are reading the user input, next you are overriding the information, because you are storing in variables instead of array.每次读取用户输入时,接下来都会覆盖信息,因为您存储在变量而不是数组中。
Create an object like:创建一个对象,如:
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); }
...
}
Then, create an Product [] productArr = new Product[anyLength];
然后,创建一个
Product [] productArr = new Product[anyLength];
Now when you check if the input is A005 for example, you would store the object with the following information:现在,当您检查输入是否为 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;
}
Then when you need to print them, you just access by the index productArr[j].getAmount()
for example.然后,当您需要打印它们时,您只需通过索引
productArr[j].getAmount()
例如。
Try as a recommended, But in the future if you want a better approach, it would be better to use a Map<String, Product> to map the A001 to your product, A002 ..., A003, ... and then when you need to print them you just get by key.建议尝试,但将来如果您想要更好的方法,最好使用 Map<String, Product> 将 A001 映射到您的产品,A002 ..., A003, ... 然后当你需要打印它们,你只是通过钥匙获得。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.