繁体   English   中英

在Java中使用多维数组

[英]Using Multidimensional arrays in Java

我正在尝试向用户显示葡萄酒菜单以供选择。 葡萄酒类型(雷司令,霞多丽等)具有多种葡萄酒。 我想向用户显示整个酒菜单,然后他们输入1-4以指示他们想要的类型,从而向他们显示该列的所有行。 然后,他们可以键入1-3以选择所需的行。 我的问题是,我不知道该怎么做。 此外,用户最多可以执行16次此操作,或者直到他们希望退出为止。 不过,这只是一种方法。 任何可以帮助的人将不胜感激。

http://puu.sh/lT5r5/4b7fd3262d.png (提示)

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 String getWineType(String wineTypes[][]){
  for(i=0; i<wineTypes[0].length;i++){
     for(int j=0; j<wineTypes.length; j++){
        JOptionPane.showMessageDialog(null, wineTypes[i][j]);
   }
  }




return wineTypes[][];
}
}

您正在声明2D数组很好,但是您的方法需要重新设计。 您为它提供了一个字符串返回类型,但未返回任何内容,请将其更改为void。

获取输入:

// somewhere before the getWineType() call
int i = Integer.parseInt(JOptionPane.showInputDialogue("Enter First number: ")
// if you want to list all the wines in the sub query at this point put a method here to do so
int j = Integer.parseInt(JOptionPane.showInputDialogue("Enter Second number: ")
JOptionPane.showMessageDialog(wineTypes[i][j]); //this will show the selected wine

至于你的酒单方法。

public static void listWineTypes(wineTypes[][]) {  //void return
       listarray[][] = wineTypes[][]; 
      for(i=0; i<listarray.length;i++){  //change this
        for(int j=0; j<listarray[i].length; j++){ //and this
          JOptionPane.showMessageDialog(wineTypes[i][j]); //this will print out each wine in the list.
}

您还必须在某处调用该方法:

     getWineType(wineType[][]); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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