[英]Initializing 2D arrays in Java
选择一种葡萄酒类型(雷司令,霞多丽),然后从中显示所选列的变体。 该循环最多应该循环16次,或者直到用户指示完成为止。 这只是我现在要完成的一种方法。
我想知道我是否朝着正确的方向前进。 我已经制作了main方法,但是我想知道如何在循环内制作用于收集输入的方法(用户输入1代表雷司令,输入1代表干法,然后将其总计)。
如果有人可以帮助我解决这个问题,我将非常感激。 非常感谢。
import javax.swing.JOptionPane;
public class WineCalc{
public static void main(String[] args){
String[][]wineTypes = {
{"Riesling", "Chardonnay", "Sauvignon Blanc", "Merlot"},
{"Dry- $4.50", "Apple- $6.00", "Lime-$4.50", "Plum- $5.00"},
{"Off Dry-$4.00", "Lemon-$5.50", "Lemongrass- $6.50", "Black Cherry- $7.50"},
{"Sweet- $5.00", "Vanilla- $6.00", "Coconut- $7.00", "Chocolate- $6.00"},
};
double[][]prices = {
{4.50, 6.00, 4.50, 5.00},
{4.00, 5.50, 6.50, 7.50},
{5.00, 6.00, 7.00, 6.00},
};
int[][]counter = {
{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
};
}
public static int getWineType(String wineTypes[][]){
return wineTypes[][];
}
}
您需要在winetypes数组中添加环绕问题的qoutation标记。
String[][]wineTypes = {
{"Riesling", "Chardonnay", "Sauvignon Blanc", "Merlot"},
{"Dry- $4.50", "Apple- $6.00", "Lime-$4.50", "Plum- $5.00"},
{"Off Dry-$4.00", "Lemon-$5.50", "Lemongrass- $6.50", "Black Cherry- $7.50"},
{"Sweet- $5.00", "Vanilla- $6.00", "Coconut- $7.00", "Chocolate- $6.00"},
};
您还需要在价格数组中的条目之间添加逗号
double[][]prices = {
{4.50, 6.00, 4.50, 5.00},
{4.00, 5.50, 6.50, 7.50},
{5.00, 6.00, 7.00, 6.00}
};
最后,您需要创建方法getWineType(int wineTypes[][]);
和getMostOrdered(int wineTypes[][]);
和getCombo(int wineTypes[][]);
和printReport(int wineTypes[][]);
为了使您走上正确的道路,我将向您展示Java的面向对象的做事方式。
public class Wine{
private final String name;
private final List<Type> types = new ArrayList<>();
private final Map<String, Type> typeMap = new HashMap<>();
public Wine(String name){
this.name = name;
}
public Wine addType(String name, double price){
addType(new Type(name, price));
return this;
}
public Wine addType(Type type){
types.add(type);
typeMap.put(type.getName(), type);
return this;
}
public Wine.Type get(int index){
return types.get(index);
}
public Wine.Type get(String type){
return typeMap.get(type);
}
public boolean hasType(String type){
return typeMap.containsKey(type);
}
public int totalWineTypes(){
return types.size();
}
@Override
public String toString(){
return new StringBuilder().append("[Wine = ").append(name).append(" ").append(types).toString();
}
public class Type{
private final String name;
private double price;
public Type(String name, double price){
this.name = name;
this.price = price;
}
public String getName(){
return name;
}
public double getPrice(){
return price;
}
@Override
public String toString(){
return Wine.this.name + "[" + name + ", " + price + "]";
}
}
}
主要方法
public static void main(String[] args) {
Wine[] wines = new Wine[]{
new Wine("Riesling").addType("Dry", 4.5).addType("Off Dry", 4.0).addType("Sweet", 5.0),
new Wine("Chardonnay").addType("Apple", 6.0).addType("Lemon", 5.5).addType("Vanilla", 6.0),
};
for (Wine w : wines) {
System.out.println(w);
if(w.hasType("Apple")){
Wine.Type t = w.get("Apple");
System.out.println();
System.out.println(t.getName() + " -> " + t.getPrice());
System.out.println(t);
}
}
}
运行时会显示
[Wine = Riesling [Riesling[Dry, 4.5], Riesling[Off Dry, 4.0], Riesling[Sweet, 5.0]]
[Wine = Chardonnay [Chardonnay[Apple, 6.0], Chardonnay[Lemon, 5.5], Chardonnay[Vanilla, 6.0]]
Apple -> 6.0
Chardonnay[Apple, 6.0]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.