在下面的代码中,我试图将名称、成本和优先级捕获到一个对象中。我使用 for 循环创建 7 个对象并将每个对象添加到名为 itemData 的 ArrayList 中。
我认为我的代码不会在每次运行循环时将名称、成本和优先级保留到项目中。非常感谢任何建议。
import java.util.*;
public class Main {
public static List<ItemData> itemData = new ArrayList<ItemData>();
public static void main(String[] args) {
int i=0;
//String name1;
//int priority1;
//double cost1;
String[] item = new String[7];
for (i=0; i<item.length; i++) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter item name " + i);
String name = keyboard.next();
Scanner keyboard2 = new Scanner(System.in);
System.out.println("Enter the price of item " + i);
double cost = keyboard2.nextDouble();
Scanner keyboard3 = new Scanner(System.in);
System.out.println("Enter Priority Number " + i);
int priority = keyboard3.nextInt();
ItemData grocItem = new ItemData(name, cost, priority);
itemData.add(grocItem); // add grocery items to itemData ArrayList
};
System.out.println(item.name)
// System.out.println(+ grocItem);
}
//How do I add grocItem to an Array list of other grocItems (6 grocItems from user input array item)
//Main.itemData.add(groclist);
}
Itemdata class:
public class ItemData{
public ItemData(String name, double cost, int priority){
// Main.groclist.add(grocItem);