尝试使用数组来存储用户输入的产品详细信息。 [英] Trying to do an array to store product details entered by user.
问题描述
您好,
我是Java新手....我正在尝试用户输入产品详细信息然后将其存储在一个数组,以便在用户需要时,他可以看到列出的产品。但是,我发现很难看到如何做阵列。有人可以看看代码并解释我做错了吗?
提前谢谢
我尝试过:
Hello,
Im a new to Java.... I am trying to do a program where user enters details of a product and then I store them in an array so that when the user wants, he can see the products listed. However, I am finding it hard to see how to do the array. Can someone please take a look at the code and explain what I did wrong?
Thank you in advance
What I have tried:
import java.util.Scanner;
public class pr {
public static void main (String[] args){
Scanner in = new Scanner (System.in);
int cost, sell, stock, vat;
String description, id;
String [] productDetails = "not";
System.out.println("___________________");
System.out.print("Enter product's unique identification number: ");
productDetails[0] = in.next();
System.out.print("Enter product's description: ");
productDetails[1] = in.next();
System.out.print("Enter product's costing price: ");
productDetails[2] = in.toString();
System.out.print("Enter product's selling price: ");
productDetails[3] = in.toString();
System.out.print("Enter quantity of the product: ");
productDetails[4] = in.toString();
System.out.print("Enter VAT cost of product: ");
productDetails[5] = in.toString();
System.out.print(productDetails);
}
}
推荐答案
你应该创建一个这样的类
这个类将描述每个产品属性的详细信息
You should create a class like this
This class will describe the details of the property of each product
class ProductDetails {
public String idNo;
public String description;
public double cost;
public double price;
public int Quantity;
}
声明一个 ProductDetails 的数组
Declare an array of ProductDetails
ProductDetails[] pdr = new ProductDetails[n];
在此运行for循环
Run the for loop on this
for(int i=0;i<n;i++) {
pdr[i]=readEachProductDetails();
}
您还需要定义
You also need to define
read_each_product_details
ProductDetails readEachProductDetails () {
ProductDetails productDetails = new ProductDetails();
// TODO your code to read the product details
return productDetails;
}
并将所有这些组合在一起
And assemble all this together
这篇关于尝试使用数组来存储用户输入的产品详细信息。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!