[英]Creating an Array from multiple user inputs of different types
How I would get the 3 items from user input Item (name, cost, priority) into one object item and place that object item into an array of other objects? 如何从用户输入项目(名称,成本,优先级)中将3个项目放入一个对象项目中,并将该对象项目放入其他对象的数组中?
public class Main {
public static void main(String[] args) {
//here are my variables.
//I'm pretty sure a few of these should be defined
// in their own item class.
int i=0;
String name1;
int priority1;
double cost1;
//String[] shoppingList = new String[7];
String[] item = new String[7];
// I am able to grab input from the user for all 3 elements of
// an item object (name, cost, priority)
// I am missing how to save these 3 elements into an item object
// and create an array for each item object
for (i=0; i<item.length;i++) {
//I want to capture name, priority, and cost of each item
// and add each item as an in
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();
"How I would get the 3 items from user input Item (name, cost, priority) into one object item" “我如何从用户输入项目(名称,成本,优先级)中获取3个项目到一个对象项目”
If I understand you correctly, the solution would be to make a new class. 如果我理解正确,那么解决方案就是创建一个新课程。 Call it whatever is relevant for what you're doing 称之为与你正在做的事情相关的任何事情
class Data {
String name;
double cost;
int priority;
Data(String name, double cost, int priority) { //Set variables }
}
Then you can create a new "Data" class with your relevant information with: 然后,您可以使用以下相关信息创建一个新的“数据”类:
Data myObject = new Data("Input1", 37.50, 2);
Just for example, with random information. 例如,随机信息。 You can make as many of these unique objects as you'd like, and add them to an array or ArrayList depending on what you want to do: 您可以根据需要制作尽可能多的这些唯一对象,并根据您要执行的操作将它们添加到数组或ArrayList中:
Data[] myArray = new Data[100];
//or
List<Data> myList = new ArrayList<Data>();
You could try a custom class: 您可以尝试自定义类:
public class Item {
private String name;
private double price; // you should probably use an 'int' for this
// it would be the amount of cents
// that would avoid rounding errors
private int priority;
public Item(String name, double price, int priority) {
this.name = name;
this.price = price;
this.priority = priority;
}
// (getters and setters)
public String getName() {
return name;
}
...
}
Item milk = new Item("milk", 19.99, 3); // wow, expensive milk :P
Item[] items = [milk, new Item("cheese", 99.99, 2), ...];
What you could do is create a class, and use the constructor to help pass parameters. 你可以做的是创建一个类,并使用构造函数来帮助传递参数。 You could then make an instance of this class, and use it to set the parameters to whatever you need (ie the name, price, etc.). 然后,您可以创建此类的实例,并使用它将参数设置为您需要的任何内容(即名称,价格等)。 Then accept the user input using the Scanner
, and what I would do, is make an ArrayList
of (I'll call it ItemData). 然后使用Scanner
接受用户输入,我将做的是制作一个ArrayList
(我将其称为ItemData)。 Then, add the instance to the ArrayList
using .add(this)
. 然后,使用.add(this)
将实例添加到ArrayList
。 Here's the code for each step: 这是每个步骤的代码:
Pass the parameters in the constructor: 传递构造函数中的参数:
public class ItemData{ public ItemData(String name, double cost, int priority){ } }
Make a new instance of ItemData
in the Main
class at the end of the input: 在输入结尾的Main
类中创建一个ItemData
的新实例:
ItemData itemData = new ItemData(name, cost, priority);
Make the ArrayList
field (make sure you import java.util.ArrayList
and java.util.List
) : 创建ArrayList
字段(确保导入java.util.ArrayList
和java.util.List
):
List<ItemData> dataStorage = new ArrayList<ItemData>();
Add the instance of ItemData
to the ArrayList
into the ItemData
constructor: 将ItemData
的实例添加到ArrayList
到ItemData
构造函数中:
Main.(ArrayList name here).add(this);
Your final code should look like this: 您的最终代码应如下所示:
public class ItemData{
public ItemData(String name, double cost, int priority){
Main.itemData.add(this);
}
}
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 x = new ItemData(name, cost, priority);
}
}
}
Your item class: 你的项目类:
class Item {
String name;
double cost;
int priority;
Item(String name, double cost, int priority) {
this.name = name;
this.cost = cost;
this.priority = priority;
}
}
"An Array of objects" is a misnomer in Java, because arrays have fixed-length elements. “对象数组”在Java中是用词不当,因为数组具有固定长度的元素。 I do not think it's safe to assume all your Item objects will be the same size. 我认为假设所有Item对象的大小都相同是不安全的。
Use a "List" - or in Java, an "ArrayList" 使用“List” - 或者在Java中,使用“ArrayList”
ArrayList<Item> myCart = new ArrayList<Item>();
Create the item object: 创建项目对象:
Item myItem = new Item('cheese', 42.12, 1); //cheese is good
Append the item to the shopping cart: 将商品附加到购物车:
myCart.add(myItem);
http://javarevisited.blogspot.com/2011/05/example-of-arraylist-in-java-tutorial.html http://javarevisited.blogspot.com/2011/05/example-of-arraylist-in-java-tutorial.html
http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html Creating an Arraylist of Objects http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html 创建对象的Arraylist
You should try making a User Defined Type which acts as a data structure to hold the values you desire. 您应该尝试创建一个用户定义类型作为数据结构来保存您想要的值。
Since java is an object oriented language, you can create a user defined type with a class. 由于java是面向对象的语言,因此您可以使用类创建用户定义的类型。 Below, I created a class called Item with three member data variables. 下面,我创建了一个名为Item的类,其中包含三个成员数据变量。 This class is a blueprint for all Item type objects. 此类是所有Item类型对象的蓝图。 Whenever you create a new item object, it will have its own unique copies of the member data variables. 无论何时创建新的项目对象,它都将拥有自己的成员数据变量的唯一副本。
For sake of simplicity, I did not encapsulate the member data variables, but for future programs you should declare member data variables private and provide an interface to access/modify them. 为简单起见,我没有封装成员数据变量,但是对于将来的程序,您应该声明成员数据变量是私有的,并提供访问/修改它们的接口。 Leaving them public here allows me the convenience of accessing them with the dot (.) operator. 将它们公开在这里使我可以方便地使用点(。)运算符访问它们。
See the following code for a working example 有关工作示例,请参阅以下代码
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Item {
public String name;
public int priority;
public double cost;
public static void main(String[] args) {
int itemAmount = 7;
List<Item> itemList = new ArrayList<Item>();
for (int i=0; i < itemAmount; i++) {
Item itemToCreate = new Item();
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter item name " + i);
itemToCreate.name = keyboard.next();
Scanner keyboard2 = new Scanner(System.in);
System.out.println("Enter the price of item " + i);
itemToCreate.cost = keyboard2.nextDouble();
Scanner keyboard3 = new Scanner(System.in);
System.out.println("Enter Priority Number " + i);
itemToCreate.priority = keyboard3.nextInt();
itemList.add(itemToCreate);
} // end for
for (int i=0; i < itemList.size(); i++) {
Item tempItem = itemList.get(i);
System.out.println("Item " + tempItem.name + " has cost " + tempItem.cost + " with priority " + tempItem.priority);
}
} // end main
} // end class
You should notice I replaced your original declaration with an ArrayList data structure. 您应该注意到我用ArrayList数据结构替换了您的原始声明。 This is a dynamic data structure that can grow as you insert items into it. 这是一个动态数据结构,可以在向其中插入项目时增长。 You should take on the challenge of allowing for unlimited amount of user input and to account for an unknown quantity of Item objects in your list. 您应该接受允许无限量用户输入的挑战,并在列表中考虑未知数量的Item对象。
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.