简体   繁体   English

如何循环返回并与用户输入一起使用

[英]How to loop back through and use with user input

So my question is what would be a good way to get a yes no response from a user using j option pane and then if that answer is correct then looping back through my program. 所以我的问题是,使用j选项窗格从用户那里获得“是,否”响应的好方法是什么,然后,如果该答案正确,则循环回我的程序。 Right now my program asks you if you want to sell or buy stuff and then it goes from there. 现在,我的程序询问您是否要出售或购买东西,然后从那里开始。 Ideally I would like to ask if you would like to bargain and if you say no the program does not bargain anymore but if you say yes then it asks you if you want to buy or sell, then after that whole thing is done then I would like it to ask if you would like to barter again. 理想情况下,我想问您是否想讨价还价,如果您说不,该程序将不再讨价还价,但如果您说是,那么它将询问您是否要购买或出售,然后在完成全部操作之后,我会想要问您是否想再进行一次易货交易。 Any help is useful thank you. 任何帮助都是有用的,谢谢。

    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);


}

You could use a do/while. 您可以使用do / while。 Keep iterating as long as the user wants to. 保持迭代,只要用户想要。 Im not sure what ogresponse contains but im assuming 1 for no? 我不确定包含什么,但我假设1否? Hope this helps. 希望这可以帮助。

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);

Try following approach: 尝试以下方法:

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);

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

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