2 回答

TA貢獻1802條經驗 獲得超4個贊
創建一個自定義對象,可能稱為Combo
. 那么該Combo
對象將具有兩個屬性:
描述
價錢
您可以Combo
通過傳入Description
和Price
作為參數來創建對象。然后您將添加一個getDescription()
andgetPrice()
方法,以便您可以訪問該類的數據。
這是創建自定義對象的基礎知識。
public class Combo
{
private String description;
private double price;
public Combo(String desctiption, double price)
{
this.description = description;
this.price = price;
}
public String getDesctiption()
{
return description;
}
public double getPrice()
...
}
我會讓你填空。

TA貢獻1812條經驗 獲得超5個贊
如果您是初學者,那么您可能還沒有學習過課程。
另一種方法是使用數組。
String[] orders = new String[4];
orders[0] = "1 Pizza large 14\", 12 chicken wings, and 1 medium fries;24.99";
// TODO: set orders 1..3
然后當你打印收據時,你可以做
for (String o : orders) {
String parts = o.split(";"); // used ; because there are commas within the data
String description = parts[0];
String price = parts[1];
// TODO: print them
}
否則,你會被單個變量困住
String combo1Description;
int combo1Price;
...
哪個不容易循環
添加回答
舉報