简体   繁体   English

用Java初始化2D数组

[英]Initializing 2D arrays in Java

I am trying to make a menu like this for a user 我正在尝试为用户制作这样的菜单 在此处输入图片说明

to select a wine type (riesling, chardonnay) and then from that, it shows them the variations of the column the selected. 选择一种葡萄酒类型(雷司令,霞多丽),然后从中显示所选列的变体。 The loop is supposed to cycle at most 16 times, or until the user indicates they're done. 该循环最多应该循环16次,或者直到用户指示完成为止。 This is just one method that I am trying to complete right now. 这只是我现在要完成的一种方法。

I wanted to know if I was heading in the right direction. 我想知道我是否朝着正确的方向前进。 I have made the main method but I wanted to know how to make the method for gathering the input(user enters 1 for riesling, 1 for dry, then it totals it) all inside of a loop. 我已经制作了main方法,但是我想知道如何在循环内制作用于收集输入的方法(用户输入1代表雷司令,输入1代表干法,然后将其总计)。

I would really appreciate it if someone could just help me figure this out. 如果有人可以帮助我解决这个问题,我将非常感激。 Thank you so much. 非常感谢。

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[][];
}
}

you need to add qoutation marks areound the worrds in the winetypes array. 您需要在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"},
                    };

also you need to add commas between the entries in the prices array 您还需要在价格数组中的条目之间添加逗号

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}
                 };

finaly you need to create the methods getWineType(int wineTypes[][]); 最后,您需要创建方法getWineType(int wineTypes[][]); and getMostOrdered(int wineTypes[][]); getMostOrdered(int wineTypes[][]); and getCombo(int wineTypes[][]); getCombo(int wineTypes[][]); and printReport(int wineTypes[][]); printReport(int wineTypes[][]);

Just to put you more on the right track I will show you Java's Object Oriented way of doing things. 为了使您走上正确的道路,我将向您展示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 + "]";
        }
    }
}

main method 主要方法

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);
        }

    }
}

It will display this when run 运行时会显示

[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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM