use*_*595 4 java arrays class object
如何从用户输入项目(名称,成本,优先级)中将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();
Run Code Online (Sandbox Code Playgroud)
"我如何从用户输入项目(名称,成本,优先级)中获取3个项目到一个对象项目"
如果我理解正确,那么解决方案就是创建一个新课程.称之为与你正在做的事情相关的任何事情
class Data {
String name;
double cost;
int priority;
Data(String name, double cost, int priority) { //Set variables }
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用以下相关信息创建一个新的"数据"类:
Data myObject = new Data("Input1", 37.50, 2);
Run Code Online (Sandbox Code Playgroud)
例如,随机信息.您可以根据需要制作尽可能多的这些唯一对象,并根据您要执行的操作将它们添加到数组或ArrayList中:
Data[] myArray = new Data[100];
//or
List<Data> myList = new ArrayList<Data>();
Run Code Online (Sandbox Code Playgroud)