我试图向用户展示可供选择的 Wine 菜单。 Wine 类型(雷司令、霞多丽等)有不同的 Wine 品种。我想向用户展示整个 Wine 菜单,然后他们输入 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[][];
}
}
最佳答案
您声明您的二维数组很好,但您的方法需要重新设计。您给它一个字符串返回类型但不返回任何内容,请将其更改为 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
至于你列出 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[][]);
关于java - 在 Java 中使用多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34244475/