所以我的问题是使用j选项窗格从用户那里获得“是”没有响应的好方法,然后如果答案是正确的,那么循环回我的程序。现在,我的程序会询问您是否要出售或购买东西然后从那里开始。理想情况下,我想问一下你是否愿意讨价还价,如果你说不,程序不再讨价还价,但如果你说是,那么它会问你是否要买或卖,然后完成后我会做喜欢问你是否愿意再次进行易货交易。任何帮助都很有用,谢谢。
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Item> list = new ArrayList<Item>();
list.add(new Item("Ketchup", 1.00, 10, 2.00, itemType.FOOD));
list.add(new Item("Mayo", 2.00, 20, 3.0, itemType.FOOD));
list.add(new Item("Bleach", 3.00, 30, 4.00, itemType.CLEANING));
list.add(new Item("Lysol", 4.00, 40, 5.00, itemType.CLEANING));
do{
int ogresponse = JOptionPane.showConfirmDialog(null,
"Would you like to barter", "Please select",
JOptionPane.YES_NO_OPTION);
String response = JOptionPane.showInputDialog("Would you like to buy or sell items?").toLowerCase();
if (response.equals("sell")) {
String name_Item = JOptionPane.showInputDialog("What would you like to sell (options: Ketchup, Mayo, Bleach, or Lysol?").toLowerCase();
String qty_Amount = JOptionPane.showInputDialog("How much of said item would you like to sell?").toLowerCase();
int qty_num = Integer.parseInt(qty_Amount);
sell(list, name_Item, qty_num);
}else if (response.equals("buy")) {
String name_Item = JOptionPane.showInputDialog("What would you like to buy (options: Ketchup, Mayo, Bleach, or Lysol?").toLowerCase();
String qty_Amount = JOptionPane.showInputDialog("How much of said item would you like to buy?").toLowerCase();
int qty_num = Integer.parseInt(qty_Amount);
buy(list, name_Item, qty_num);
}while(ogrepsonse != 0);
String output = "";
for(Item i : list) {
int everything = i.getQty();
String everything2 = i.getName().toString();
output += everything +" "+ everything2 + "\n";
}
JOptionPane.showMessageDialog(null, "Your current balance is: $" + myBalance + "\n" + "Current stock:" + "\n" + output);
}
答案 0 :(得分:0)
你可以使用do / while。只要用户想要继续迭代。我不确定ogresponse包含什么,但我假设1为否?希望这会有所帮助。
do{
int ogresponse = JOptionPane.showConfirmDialog(null,
"Would you like to barter", "Please select",
JOptionPane.YES_NO_OPTION);
System.out.println(ogresponse);
String response = JOptionPane.showInputDialog("Would you like to buy or sell items?").toLowerCase();
if (response.equals("sell")) {
String name_Item = JOptionPane.showInputDialog("What would you like to sell (options: Ketchup, Mayo, Bleach, or Lysol?").toLowerCase();
String qty_Amount = JOptionPane.showInputDialog("How much of said item would you like to sell?").toLowerCase();
int qty_num = Integer.parseInt(qty_Amount);
sell(list, name_Item, qty_num);
}else if (response.equals("buy")) {
String name_Item = JOptionPane.showInputDialog("What would you like to buy (options: Ketchup, Mayo, Bleach, or Lysol?").toLowerCase();
String qty_Amount = JOptionPane.showInputDialog("How much of said item would you like to buy?").toLowerCase();
int qty_num = Integer.parseInt(qty_Amount);
buy(list, name_Item, qty_num);
}
}while(ogresponse !=1);
答案 1 :(得分:0)
尝试以下方法:
do {
ogresponse = JOptionPane.showConfirmDialog(null, "Would you like to barter", "Please select", JOptionPane.YES_NO_OPTION);
System.out.println(ogresponse);
String response = JOptionPane.showInputDialog("Would you like to buy or sell items?").toLowerCase();
if (response.equals("sell")) {
String name_Item = JOptionPane.showInputDialog("What would you like to sell (options: Ketchup, Mayo, Bleach, or Lysol?").toLowerCase();
String qty_Amount = JOptionPane.showInputDialog("How much of said item would you like to sell?").toLowerCase();
int qty_num = Integer.parseInt(qty_Amount);
sell(list, name_Item, qty_num);
} else if (response.equals("buy")) {
String name_Item = JOptionPane.showInputDialog("What would you like to buy (options: Ketchup, Mayo, Bleach, or Lysol?").toLowerCase();
String qty_Amount = JOptionPane.showInputDialog("How much of said item would you like to buy?").toLowerCase();
int qty_num = Integer.parseInt(qty_Amount);
buy(list, name_Item, qty_num);
}
} while (ogresponse != 1);