簡體   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