如何向用户询问某些内容然后保存答案并反复再次询问相同内容

时间:2012-10-19 23:13:16

标签: java loops while-loop nested

好的以下代码(我是新手,请耐心等待我)我希望能够向用户询问问题:

System.out.println("Will the customer be adding any options to the order?");
System.out.println("1. Bed Frame ($39.99)");
System.out.println("2. Pillows ($59.99)");
System.out.println("3. Blankets ($129.99)");
System.out.println("4. No Options added. Enter No");
  1. 客户将回答1-4然后进行循环并询问客户是否想要添加任何其他内容/您确定不想添加任何内容。如果没有客户被发送到另一个类(更多问题)。如果是的话,客户会被问到同样的“客户......”会再次提出问题。
  2. 客户添加让我们说项目1并经历一个循环,并询问客户是否想要添加任何其他内容/您确定不想添加任何内容。如果没有发送到另一个类。如果是的话,客户会被问到同样的“客户......”问题。一旦用户完成了回答问题,答案将被保存并打印在writeOutput()行中,以及客户添加的产品的名称和价格。
  3. 我知道这是一个循环,但我不知道如何实现这种循环。任何人都可以帮助我并填补空白吗?谢谢。以下是完整的代码

    import java.util.Scanner;
    public class addons
    

    {

        private int Option;
        private String Bed_Frame, Pillows, Blankets, No_Options_Added;
        private String Name;
        private double Price;
        public void readInput()
        {
    
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Will the customer be adding any options to the order?");
             System.out.println("1. Bed Frame ($39.99)");
          System.out.println("2. Pillows ($59.99)");
             System.out.println("3. Blankets ($129.99)");
             System.out.println("4. No Options added. Enter No");
            Option = keyboard.nextInt();
    
           switch (Option)
                {
                    case 1:
                       Name = "Bed Frame";
                        Price = 39.99;
                        break;
                    case 2:
                       Name = "Pillows";
                       Price = 59.99;
                        break;
                     case 3:
                       Name = "Blankets";
                       Price = 129.99;
                        break;
                     case 4:
                     Name = "No Options Added";
                     System.out.println("No options added");
                       Price = 0;
                        break;
                }
        }
    
        public void writeOutput()
        {
            System.out.println(Name);
        }
    

    }

1 个答案:

答案 0 :(得分:2)

只需添加一个while(true) { ... }块,您就可以在问题后添加一个条件 - 如果答案是“是” - 请使用break(不在switch-case块中!)来完成重复循环:

 public void readInput() {

        Scanner keyboard = new Scanner(System.in);
        while (true) {  //ADDED THE WHILE LOOP
           System.out.println("Will the customer be adding any options to the order?");
           System.out.println("1. Bed Frame ($39.99)");
           System.out.println("2. Pillows ($59.99)");
           System.out.println("3. Blankets ($129.99)");
           System.out.println("4. No Options added. Enter No");
           Option = keyboard.nextInt();
           switch (Option)
              {
               case 1:
                 Name = "Bed Frame";
                 Price = 39.99;
                 break;
               case 2:
                 Name = "Pillows";
                 Price = 59.99;
                 break;
               case 3:
                 Name = "Blankets";
                 Price = 129.99;
                 break;
               case 4:
                 Name = "No Options Added";
                 System.out.println("No options added");
                 Price = 0;
                 break;
            }

      //ADDED EXIT POINT WHEN TO EXIT:
      System.out.println("Is that All?");
      //read an answer
      if (*answer was yes*) break;

      } //ADDED A CLOSING BLOCK
}

阅读答案并确定条件应该是一个练习 - 因为它似乎是一个教程任务,我不想破坏乐趣! :)