我在陣列和用戶回圈中有點掙扎。例如,我有這個問題(忽略 previewOrder 方法中的內容,我正在嘗試一些東西):
public class Ex1_19 {
final static String NAMES[]= {"Spa reine 25 ","Bru plate 50","Bru pét 50",
"Pepsi","Spa orange", "Schweppes Tonic","Schweppes Agr","Ice Tea","Ice Tea Pêche",
"Jus d'orange Looza", "Cécémel", "Red Bull","Petit Expresso","Grand Expresso","Café décaféiné ",
"Lait Russe ","Thé et infusions","Irish Coffee ","French Coffee ","Cappuccino","Cécémel chaud",
"Passione Italiano","Amour Intense", "Rhumba Caliente ","Irish Kisses ","Cuvée Trolls 25",
"Cuvee Trolls 50","Ambrasse-Temps 25","Ambrasse-Temps 50 ","Brasse-Temps Cerises 25",
"Brasse-Temps Cerises 50","La Blanche Ste Waudru 25","Blanche Ste Waudru 50",
"Brasse-Temps citr 25","Brasse-Temps citr 50","Spaghetti Bolo ","Tagl Carbonara",
"Penne poulet baslc ","Tagl American","Tagl saum"};
final static double NETPRICES[]= {2.2, 2.3,3.9,2.2,2.2,2.6,2.6,2.6,2.6,2.6,2.6,4.5,2.2,2.2,2.2,2.5,2.5,7.0,7.0,2.8,2.8,6.2,6.2,6.2,6.2,
2.9,5.5,2.7,5.1,3.1,5.8,2.6,4.9,2.6,4.9,10.8,11.2,12.2,14.5,16.9};
public static void main(String[] args) {
int Order[][]={{3,2},{1,3},{12,4},{37,1},{36,3},{0,0},{0,0},{0,0}, {0,0}};
previewOrder(Order);
}
public static void previewOrder(int order[][]) {
int i = 0;
int j = 0;
while(i < order.length && j < order.length) {
System.out.println(NAMES[i]);
i ;
j ;
}
}
}
我的結果必須是這樣的,但包含“order”陣列中的內容:
Bru pét 50 3.9 2 7,80 Spa reine 25 2.2 3 6,60 Red Bull 4.5 4 18,00 Tagl Carbonara 11.2 1 11,20 Spaghetti Bolo 10.8 3 32,40
在我的練習中,我必須使用 while 回圈,并且必須將順序陣列放入我的方法引數中。我不知道如何讓他們都溝通。
抱歉,如果這個問題已經在其他地方得到回答,但我不知道如何搜索它。
編輯:我知道 Orders 不使用從零開始的索引,而是從 1 開始。這可能是因為“Order”應該是一個用戶條目。陣列的第一個數字就像一個飲料編號。
我對預期的輸出不是很清楚。Bru pét 50 (NAME[3]) 3.9 (NETPRICE[3]) 2 (Order[][2]) 7.80 NETPRICE[3] * Order[][2] 并且每次出現在 Order 中
uj5u.com熱心網友回復:
該Order陣列(順便說一句:應命名為order或orders)顯然包含對已訂購商品及其數量的參考。它是一個二維陣列,就像一個有兩列的表格。
來自 Order[0] {3,2} 的“Bru pét 50 3.9 2 7,80”的預期輸出表示第一個元素 (Order[0][0]) 是對專案名稱 (from NAMES)的參考和價格(從NETPRICES)。每個“行”的第二個值是專案數量 (2),最后是計算的總數。
出于未知原因,Orders 不使用從零開始的索引,而是從 1 開始。因此 ORDER[0] 的值為 {3,2} 實際上是指 NAMES[2] 和 NETPRICES[2]。在從 NAMES 和 NETPRICES 選擇正確的專案時需要考慮到這一點。
無論如何:這就是您的方法的樣子。您仍然需要根據需要調整輸出。
public static void previewOrder(int order[][]) {
for (int i = 0; i < order.length; i ) {
int index = order[i][0] - 1;
if (index < 0 || index > NAMES.length || index > NETPRICES.length) {
continue;
}
String name = NAMES[order[i][0] - 1];
double price = NETPRICES[order[i][0] - 1];
int amount = order[i][1];
double total = amount * price;
System.out.println(
name " " price " " amount " " total
);
}
}
uj5u.com熱心網友回復:
嘗試這個。
public static void previewOrder(int order[][]) {
Stream.of(order)
.filter(x -> x[0] != 0)
.forEach(x -> {
String name = NAMES[x[0] - 1];
int unit = x[1];
double price = NETPRICES[x[0] - 1];
System.out.printf("%s %.1f %d %.2f%n",
name, price, unit, price * unit);
});
}
輸出:
Bru p?t 50 3.9 2 7.80
Spa reine 25 2.2 3 6.60
Red Bull 4.5 4 18.00
Tagl Carbonara 11.2 1 11.20
Spaghetti Bolo 10.8 3 32.40
或者
public static void previewOrder(int order[][]) {
for (int[] row : order) {
if (row[0] == 0)
continue;
String name = NAMES[row[0] - 1];
int unit = row[1];
double price = NETPRICES[row[0] - 1];
System.out.printf("%s %.1f %d %.2f%n",
name, price, unit, price * unit);
}
}
uj5u.com熱心網友回復:
當我看到緊耦合陣列時,我想到創建一個類。
public class Product {
String name;
double unitPrice;
public Product () {
name = "*** Unnamed Product ***";
unitPrice = 0.0;
}
public Product (String name, double unitPrice) {
this.name = name;
this.unitPrice = unitPrice;
}
public String getName () { return name; }
public double getPrice () { return unitPrice; }
public String toString () { return name " @ " unitPrice " ea.";}
}
然后,您可以在之前有兩個陣列的地方擁有一個陣列:
public class Ex1_19 {
static final Product [] productList = {
new Product("Spa reine 25 ", 2.2),
new Product("Bru plate 50", 2.3),
new Product("Bru pét 50", 3.9),
new Product("Pepsi", 2.2),
new Product("Spa orange", 2.2),
new Product("Schweppes Tonic", 2.6),
new Product("Schweppes Agr", 2.6),
new Product("Ice Tea", 2.6)
// and so on
};
}
沒有格式化輸出的預覽順序方法可能如下所示:
public static void previewOrder (int[][] order) {
double total = 0.0;
double itemCount = 0;
int lineNum = 0;
for (int i = 0; i < order.length; i) {
int row = order[i][0] - 1;
if (row < 0) {
break;
}
int qty = order [i][1];
double amt = productList [row].getPrice () * qty;
System.out.println ( lineNum " " productList[row].getName()
" " productList[row].getPrice () " " qty
" " amt);
itemCount = qty;
total = amt;
}
System.out.println ("===\nTotal: " total " for " itemCount " items.");
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/535335.html
標籤:爪哇数组方法
下一篇:對隨機整數陣列進行排序的最快方法
