我试图创建一个让用户输入1-4的模块来选择列(riesling,chardonnay),然后从该列中选择一行(他们想要的变体)
我遇到的问题:如何为其制作for循环?我的wineTypes数组是字符串,所以当我返回wineTypes [] []时,它实际上给了我一个错误,我应该返回字符串吗?
我只需要一些模块帮助来获取他们的选择,并跟踪另一个模块中的总数/最多。
我的代码:
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[][]){
for(i=0; i<wineTypes[0].length;i++){
for(int j=0; j<wineTypes.length; j++){
JOptionPane.showMessageDialog(null, wineTypes[i][j]);
}
}
return wineTypes[][];
}
}
答案 0 :(得分:1)
考虑到我与OP的简短的聊天,其中更详细地解释了他所遇到的问题,我已经决定了帮助他的最好方法有一个例子可以说明如何解决他在作业中遇到的问题,同时注意不要给出作业的答案:
public static String[] wineTypes = {"Riesling", "Chardonnay", "Sauvignon Blanc", "Merlot"};
public static String[][] wineVariations = {
{"Dry- $4.50", "Off Dry-$4.00", "Sweet- $5.00"},
{"Apple- $6.00", "Lemon-$5.50", "Vanilla- $6.00"},
{"Lime-$4.50", "Lemongrass- $6.50", "Coconut- $7.00"},
{"Plum- $5.00", "Black Cherry- $7.50", "Chocolate- $6.00"}
};
public static void main(String[] args) {
do {
String wineType = (String) JOptionPane.showInputDialog(null,
"Select wine type:",
"Input - Wine Type - (Column)",
JOptionPane.QUESTION_MESSAGE,
null,
wineTypes,
wineTypes[0]);
if (wineType == null) {
System.out.println("NULL!");
} else {
System.out.println(wineType);
}
/*
Do "the same" for the variations within the selected type.
Tip: If you can use ArrayLists instead of Arrays, it will make it a lot easier to get the indexes of the choices...
...This, in turn, would make the second loop, and the processing of the prices, much easier...
...Just be careful with the last rule in your assignment; make sure it was covered, or don't use it.
Tip: You will need a loop to get the correct array of options...
...And you'll need to contain that loop within a function, and return only the result, in order to use it as parameter to the JOptionPane.
*/
/*
Except for the main loop, I left the handling of the "cancel" buttons to you as well.
*/
} while (JOptionPane.showConfirmDialog(null, "Continue?", "Input - Continue", JOptionPane.YES_NO_OPTION) == 0);
}