用Java初始化2D数组

时间:2015-12-11 23:42:47

标签: java arrays multidimensional-array

我正在尝试为用户enter image description here

制作这样的菜单

选择葡萄酒类型(雷司令,霞多丽),然后从中,它会显示所选列的变体。循环应该最多循环16次,或者直到用户表明它们已经完成。这只是我现在想要完成的一种方法。

我想知道我是否朝着正确的方向前进。我已经制作了主要方法,但我想知道如何制作收集输入的方法(用户输入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[][];
}
}

2 个答案:

答案 0 :(得分:1)

你需要在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[][]);

答案 1 :(得分:1)

为了让您在正确的轨道上走得更远,我将向您展示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]