我想將所有輸入值傳遞給數組中的一個元素。我試圖玩數組,但不太明白他們如何處理多個輸入值。Java將多個輸入值存儲到單個數組元素
我已閱讀Oracle和這裏,但沒有真正脫穎而出,或他們求助於數組列表。
有沒有一種方法來封裝值,然後將它們存儲到數組?
任何幫助,非常感謝。
import java.util.Scanner;
class Mobile
{
private String name;
private int number;
private int quantity;
private double cost;
public Mobile(String name, int number, int quantity, double cost)
{
this.name = name;
this.number = number;
this.quantity = quantity;
this.cost = cost;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getNumber()
{
return number;
}
public void setNumber(int number)
{
this.number = number;
}
public int getQuantity()
{
return quantity;
}
public void setQuantity(int quantity)
{
this.quantity = quantity;
}
public double getCost()
{
return cost;
}
public void setCost(double cost)
{
this.cost = cost;
}
public double getValue()
{
return cost*quantity;
}
}
public class MobilePhone
{
public static void main (String args[])
{
String itemName;
int itemNum;
int itemQuan;
double unitCost;
for(int i=0;i<5;i++)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter Item Name: ");
itemName = input.nextLine();
System.out.print("Enter Item Number: ");
itemNum = input.nextInt();
System.out.print("Enter Quantity of Item: ");
itemQuan = input.nextInt();
System.out.print("Enter Price of Single Unit: ");
unitCost = input.nextDouble();
Mobile m = new Mobile(itemName, itemNum, itemQuan, unitCost);
}
}
}
目前還不清楚你想做什麼 – Dici 2015-01-20 21:49:30
我想將itemName,itemNum,itemQuan,unitCost的輸入值存儲到一個數組中。 – 2015-01-20 21:50:28
那麼?是不是'移動[]'足夠呢? – Dici 2015-01-20 21:51:31